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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.