Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data files 1

Status
Not open for further replies.

vquick

IS-IT--Management
Jul 13, 2008
14
US
I have multiple user data files that i want to print out like the following:
ID Name Role Group Level Type Apps AUTH LTR
user1 SMITH JOHN SP 12 5 1 app1,app2 2,1 100

i have the included a example of a data file. the only columns that will change are columns 7 and 8.

i thought maybe some kind of join would work but cannot get it to work.

users1.dat:
user1:SMITH, JOHN:SP:12:5:1:app1:2:100
user1:SMITH, JOHN:SP:12:5:1:app2:1:100
 
What have you tried so far?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
sorry i have been offline since yesterday. ive tried multiple things, but here is my last failure.

for (<DAT>){
chomp;
($id,$name,$role,$wg,$auth,$lev,$app,$mode,@rest) = split(/\:/, $_);

$out_apps = $apps{"apps"}{$app};
}

for (keys %out_apps){
print "$_";
}
 
I done it as an friday exercise :)

Given is this exaple file named user.dat
Code:
user1:SMITH, JOHN:SP:12:5:1:app1:2:100
user2:JONES, DAVID:SP:12:5:1:app1:2:100
user3:MILLER, BILL:SP:12:5:1:app2:1:100
user1:SMITH, JOHN:SP:12:5:1:app2:1:100
user3:MILLER, BILL:SP:12:5:1:app1:2:100

Here is the script named user.pl which does the work
Code:
use strict;

my %data_hash=();
while (<STDIN>){
  chomp;
  my ($id,$name,$role,$grp,$lev,$type,$app,$auth,$ltr,@rest)=split(/:/, $_); 
  my @data_list=();
  my @app_list =();
  my @auth_list=();
  if (not $data_hash{$id}){
    push (@app_list, $app);
    push (@auth_list, $auth);
    push (@data_list, $name,$role,$grp,$lev,$type,\@app_list,\@auth_list,$ltr);
    #print "@data_list\n";
    # add reference to the list
    $data_hash{$id} =\@data_list
  }
  else{
    # dereference hash element list
    @data_list = @{$data_hash{$id}};
    #print "@data_list\n";
    # dereference app_list
    @app_list  = @{$data_list[5]};
    my $found = 0;
    foreach my $element (@app_list){
      if ($app eq $element){
        $found = 1;
        last;
      }
    }
    if (not $found){
      # add to list
      push(@app_list, $app);
      # add reference
      $data_list[5] =\@app_list;
    }
    #print "@app_list\n";
    # dereference auth_list
    @auth_list  = @{$data_list[6]};
    my $found = 0;
    foreach my $element (@auth_list){
      if ($auth eq $element){
        $found = 1;
        last;
      }
    }
    if (not $found){
      # add to list
      push(@auth_list, $auth);
      # add reference
      $data_list[6] =\@auth_list;
    }
    #print "@auth_list\n";

    # add reference to the list
    $data_hash{$id} =\@data_list
 
  }
} # end while

# Print out results
foreach my $key (sort keys (%data_hash)){
  # dereference list  
  my @data_list = @{$data_hash{$key}};
  my @app_list  = @{$data_list[5]};
  my $app_string = join(',', @app_list);
  my @auth_list  = @{$data_list[6]};
  my $auth_string = join(',', @auth_list);

  push(my @report_list, $key, @data_list[0..4],
          $app_string, $auth_string, @data_list[7]);
  print "@report_list\n";
}

Running the script with input from the file prints this output
Code:
D:\>perl user.pl < user.dat
user1 SMITH, JOHN SP 12 5 1 app1,app2 2,1 100
user2 JONES, DAVID SP 12 5 1 app1 2 100
user3 MILLER, BILL SP 12 5 1 app2,app1 1,2 100

I used a hash which elements are references to lists - and every list contains strings and 2 references to other lists (those with apps and auths)
I would be happy to see some better solutions from the Perl Gurus here, so I can improve my sparse Perl skills.
 
using mikroms data here is one way to do it.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Data::Dumper[/green][red];[/red]
[black][b]use[/b][/black] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%users[/blue][red];[/red]
[olive][b]while[/b][/olive][red]([/red]<DATA>[red])[/red][red]{[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
   [black][b]my[/b][/black] [blue]@d[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple]:[/purple][red]/[/red][red])[/red][red];[/red]
   [olive][b]if[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/exists.html][black][b]exists[/b][/black][/url] [blue]$users[/blue][red]{[/red][blue]$d[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red][red])[/red] [red]{[/red]
      [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$users[/blue][red]{[/red][blue]$d[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red][red]{[/red][red]'[/red][purple]Apps[/purple][red]'[/red][red]}[/red][red]}[/red],[blue]$d[/blue][red][[/red][fuchsia]6[/fuchsia][red]][/red][red];[/red]
      [black][b]push[/b][/black] [blue]@[/blue][red]{[/red][blue]$users[/blue][red]{[/red][blue]$d[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red][red]{[/red][red]'[/red][purple]AUTH[/purple][red]'[/red][red]}[/red][red]}[/red],[blue]$d[/blue][red][[/red][fuchsia]7[/fuchsia][red]][/red][red];[/red]
   [red]}[/red]
   [olive][b]else[/b][/olive] [red]{[/red]
       [gray][i]#ID    Name       Role  Group Level Type Apps      AUTH LTR[/i][/gray]
      [blue]$users[/blue][red]{[/red][blue]$d[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red] = [red]{[/red]
         [red]'[/red][purple]ID[/purple][red]'[/red]    => [blue]$d[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red],
         [red]'[/red][purple]Name[/purple][red]'[/red]  => [blue]$d[/blue][red][[/red][fuchsia]1[/fuchsia][red]][/red],
         [red]'[/red][purple]Role[/purple][red]'[/red]  => [blue]$d[/blue][red][[/red][fuchsia]2[/fuchsia][red]][/red],
         [red]'[/red][purple]Group[/purple][red]'[/red] => [blue]$d[/blue][red][[/red][fuchsia]3[/fuchsia][red]][/red],
         [red]'[/red][purple]Level[/purple][red]'[/red] => [blue]$d[/blue][red][[/red][fuchsia]4[/fuchsia][red]][/red],
         [red]'[/red][purple]Type[/purple][red]'[/red]  => [blue]$d[/blue][red][[/red][fuchsia]5[/fuchsia][red]][/red],
         [red]'[/red][purple]Apps[/purple][red]'[/red]  => [red][[/red][blue]$d[/blue][red][[/red][fuchsia]6[/fuchsia][red]][/red][red]][/red],
         [red]'[/red][purple]AUTH[/purple][red]'[/red]  => [red][[/red][blue]$d[/blue][red][[/red][fuchsia]7[/fuchsia][red]][/red][red]][/red],
         [red]'[/red][purple]LTR[/purple][red]'[/red]   => [blue]$d[/blue][red][[/red][fuchsia]8[/fuchsia][red]][/red],
      [red]}[/red]
   [red]}[/red]
[red]}[/red]	
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] Dumper \[blue]%users[/blue][red];[/red]

[teal]__DATA__[/teal]
[teal]user1:SMITH, JOHN:SP:12:5:1:app1:2:100[/teal]
[teal]user2:JONES, DAVID:SP:12:5:1:app1:2:100[/teal]
[teal]user3:MILLER, BILL:SP:12:5:1:app2:1:100[/teal]
[teal]user1:SMITH, JOHN:SP:12:5:1:app2:1:100[/teal]
[teal]user3:MILLER, BILL:SP:12:5:1:app1:2:100[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
Core (perl 5.10.0) Modules used :
[ul]
[li]Data::Dumper - stringified perl data structures, suitable for both printing and eval[/li]
[/ul]
[/tt]

Assumes the ID is unique. All you have to do is add your own file I/O and figure out how to display the output data. If you get stuck on that post back.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi KevinADC,

I guessed that it would be a better way to do the task, but now I'm truly amazed.
Unlike my "brute force" script was 73 lines long (without data), your smart script has only 34 lines (including data).
Very nice!
Now, I'm just figuring out, how it works.
:)

Thanx!
 
And thanks for the warnings pragma - I found 2 in my source.
I used Data::Dumper to view the structure I used.
Here is the structure I used:
Code:
$VAR1 = {
          'user1' => [
                       'SMITH, JOHN',
                       'SP',
                       '12',
                       '5',
                       '1',
                       [
                         'app1',
                         'app2'
                       ],
                       [
                         '2',
                         '1'
                       ],
                       '100'
                     ],
          'user3' => [
                       'MILLER, BILL',
                       'SP',
                       '12',
                       '5',
                       '1',
                       [
                         'app2',
                         'app1'
                       ],
                       [
                         '1',
                         '2'
                       ],
                       '100'
                     ],
          'user2' => [
                       'JONES, DAVID',
                       'SP',
                       '12',
                       '5',
                       '1',
                       [
                         'app1'
                       ],
                       [
                         '2'
                       ],
                       '100'
                     ]
        };
and here is the structure of KevinADC:
Code:
$VAR1 = {
          'user1' => {
                       'ID' => 'user1',
                       'Type' => '1',
                       'Apps' => [
                                   'app1',
                                   'app2'
                                 ],
                       'Level' => '5',
                       'Role' => 'SP',
                       'LTR' => '100',
                       'AUTH' => [
                                   '2',
                                   '1'
                                 ],
                       'Name' => 'SMITH, JOHN',
                       'Group' => '12'
                     },
          'user3' => {
                       'ID' => 'use
                       'Role' => 'SP',
                       'LTR' => '100',
                       'AUTH' => [
                                   '1',
                                   '2'
                                 ],
                       'Name' => 'MILLER, BILL',
                       'Group' => '12'
                     },
          'user2' => {
                       'ID' => 'user2',
                       'Type' => '1',
                       'Apps' => [
                                   'app1'
                                 ],
                       'Level' => '5',
                       'Role' => 'SP',
                       'LTR' => '100',
                       'AUTH' => [
                                   '2'
                                 ],
                       'Name' => 'JONES, DAVID',
                       'Group' => '12'
                     }
        };
 
Well, my code does not include the actual file I/O and I used Data::Dumper to print the output so really more lines of code would be needed to call it a complete solution. Your solution does not take good advantage of perls complex data capabilities but it appears you do have some experience with references. So I think after a bit of review you will figure out the code I posted without much trouble. Its just a hash of hashes/arrays, which is not even very complex.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi KevinADC,

Yes, I tried Perl already.
I had read a book and wrote at my work 1 usable script for reformatting a HTML-page and that way I have a little experience with references.

But in this Perl-Forum one can gather valuable experience.
I's probably the best forum on Tek-Tips. And this is thanks to You and other Gurus here.
:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top