This solution produces the proper results, printing the following:#!/usr/bin/perl -w use strict; $|++; my %source; my %action; my %sub_action; ## # process file while (<DATA>) { my ($source, $action, $sub_action) = split; my $source_action = $source . "||" . $action; # sub_action isn't required to appear my $source_sub_action = $source_action . "||" . $sub_action if $sub_action; $source{$source}++; $action{$source_action}++; # sub_action isn't required to appear $sub_action{$source_sub_action}++ if $source_sub_action; } ## # print statistics while (my ($source_code, $source_code_count) = each %source) { print "source code: $source_code count: $source_code_count\n"; # print actions and counts for this source code foreach my $action (keys %action) { print "action: $action count: $action{$action}\n" if $action =~ /$source_code\|\|/; } # print sub_actions and counts for this source code foreach my $sub_action (keys %sub_action) { print "sub action: $sub_action count: $sub_action{$sub_action}\n" if $sub_action =~ /$source_code\|\|/; } } __DATA__ source1 QUEUED source1 QUEUED source1 CLICK linkid1 source1 CLICK linkid1 source1 CLICK linkid2 source2 QUEUED source2 CLICK linkid1 source2 CLICK linkid1 source2 CLICK linkid2
source code: source1 count: 5 action: source1||CLICK count: 3 action: source1||QUEUED count: 2 sub action: source1||CLICK||linkid1 count: 2 sub action: source1||CLICK||linkid2 count: 1 source code: source2 count: 4 action: source2||CLICK count: 3 action: source2||QUEUED count: 1 sub action: source2||CLICK||linkid1 count: 2 sub action: source2||CLICK||linkid2 count: 1Like any other student of programming, proper results aren't enough for me. Style, efficiency, beer and fast cars are also important. I really don't like the attack of:
I guess that's why I'm here, at Seekers of Perl Wisdom.# build datastructures while (logfile) { build hash1; build hash2; build hash3; } # process the datastructures foreach key value (hash1) { foreach over keys of hash2; foreach over keys of hash3; }
In reply to Choosing the right datastructure by dug
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |