in reply to Re^2: Extracting and counting string occurences
in thread Extracting and counting string occurences
which produces a data structure similar to what this code does:while (<$fh_log>) { my ($month, $user) = /^LOGGING: \S+?-(\S+) \S+: STARTING FLUSH: (\S ++)$/ or next; $flushes{$month}{$user}++; $flushes{$month}{TOTAL}++; }
you could do this to get the output:my %flushes = ( 'Aug-2008' => { 'ces' => 5, 'cjc' => 7, 'TOTAL' => 12, }, 'Jul-2008' => { 'mhs' => 1, 'ces' => 3, 'cjc' => 4, 'TOTAL' => 8, }, );
If you want higher marks, you might want to use strictures and warnings. That will require declaring %flushes in the proper scope. You'll need to add opening the file and error checks to his code, anyway.for my $month ( keys %flushes ) { print $month . "\n"; print 'Total: ' . $flushes{$month}{TOTAL} . "\n#####\n"; for my $user ( sort keys %{ $flushes{$month} } ) { next if $user eq 'TOTAL'; printf "%-20s%3d\n", $user, $flushes{$month}{$user}; } print "\n"; }
If you'd rather sort by uses (descending) than by their user names, you could try this instead:
for my $month ( keys %flushes ) { print $month . "\n"; print 'Total: ' . $flushes{$month}{TOTAL} . "\n#####\n"; for my $user ( sort { $flushes{$month}{$b} <=> $flushes{$month}{$a} } keys %{ + $flushes{$month} } ) { next if $user eq 'TOTAL'; printf "%-20s%3d\n", $user, $flushes{$month}{$user}; } print "\n"; }
Now, is there a particular part of that you're having problems understanding or did you just want someone to write the code for you?
|
|---|