perladdict has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How to match the each line of of users for a perticular time with predeclared group.
  • Download Code

Replies are listed 'Best First'.
Re: How to match the each line of of users for a perticular time with predeclared group.
by wfsp (Abbot) on Jan 18, 2007 at 18:07 UTC
    Couple of points.

    If you want to lookup each users group then one way is to build a lookup table (a hash).

    I would print the report after all the data has been parsed building an output hash as you go.

    I've used the user names you used in your sample data to keep things simple.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %groups; $groups{'g1'} = ['a'..'g']; $groups{'g2'} = ['h'..'n']; $groups{'g3'} = ['o'..'u']; $groups{'g4'} = ['v'..'z']; my %lookup; for my $grp (keys %groups){ %{$lookup{$grp}} = map {$_ => undef} @{$groups{$grp}}; } my %output; while(my $line=<DATA>){ chomp($line); my $usr=substr($line,8,2); my $time=substr($line,0,4); my $username_string = substr($line,11); my @usernames= split ',', $username_string; for my $name (@usernames){ for my $grp (keys %lookup){ $output{$time}{$grp}++ if exists $lookup{$grp}{$name}; } } } for my $time (sort keys %output){ print "$time\t"; for my $grp (sort keys %groups){ my $logged_in = 0; $logged_in = $output{$time}{$grp} if exists $output{$time}{$grp}; print "$logged_in ($grp) users\t"; } print "are logged in\n"; } __DATA__ 1.00 50 10 x,y,z,w,u,e,g,j,k,a 1.10 50 13 x,y,b,a,g,j,k,r,,n,m,s, 1.20 50 05 c,t,g,q,f 1.50 50 08 a,t,y,w,z,x,s,b 2.00 50 14 x,y,u,b,g,f,s,a,i,o,p,c
    output:
    ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" monk.pl 1.00 3 (g1) 2 (g2) 1 (g3) 4 (g4) users logged in 1.10 3 (g1) 4 (g2) 2 (g3) 2 (g4) users logged in 1.20 3 (g1) 0 (g2) 2 (g3) 0 (g4) users logged in 1.50 2 (g1) 0 (g2) 2 (g3) 4 (g4) users logged in 2.00 5 (g1) 1 (g2) 4 (g3) 2 (g4) users logged in > Terminated with exit code 0.
    updated: tidied up the output.

      If the group assignments are mutually exclusive (no person is assigned to more than one group ... which doesn't look to be the case, as 'apatnayi' is in groups 2 and 4), rather than loop through each of the groups, it's easier to have the lookup be a mapping of the group assignments:

      my %lookup = map { my $grp = $_; map { $_ => $grp } ( @{ $groups{$grp} }; } (keys %groups); # ... and then below ... foreach my $name (@usernames) { $output{$time}{ $lookup{$name} }++; # ... or, if you have users logged in that aren't in a group # $output{$time}{ $lookup{$name} }++ if exists $lookup{$name}; # ... or, to track those users with no groups # $output{$time}{ $lookup{$name} || 'unknown' }++; }

      If it's not, then you can have the lookup be to an array of what groups they're in, so you don't have to look through each group every time:

      my %lookup = (); foreach my $grp (keys %groups) { push @{$lookup{$_}}, $grp foreach @{$groups{$grp}}; } ... foreach my $name (@usernames) { $output($time){$_}++ foreach @{$lookup{$name}}; }
Re: How to match the each line of of users for a perticular time with predeclared group.
by liverpole (Monsignor) on Jan 19, 2007 at 01:57 UTC
        keep pouring your ideas.......

    That's a very interesting signature.  How did you happen to choose it?


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      keep pouring your ideas.......

      That's a very interesting signature. How did you happen to choose it?

      Sounds familiar indeed, ++! A good .sig as well... although I'd amend to "keep pouring your ideas... with moderation"!

      That was jesuashok's .sig before his katharsis...

      --shmem