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.
output:#!/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
updated: tidied up the 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.
In reply to Re: How to match the each line of of users for a perticular time with predeclared group.
by wfsp
in thread How to match the each line of of users for a perticular time with predeclared group.
by perladdict
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |