http://www.perlmonks.org?node_id=404283


in reply to Counting hash of hashes elements

Hashes are great for statistics. In the following method, I use a string composed of the variety of keys to index the statistics:
#!/usr/bin/perl -w use strict; my %hash = ( 'a' => { 'x' => 40, 'z' => 102 }, 'b' => { 'z' => 100, 'x' => 10, 'y' => 20 }, 'c' => { 'x' => 50 }, 'd' => { 'z' => 101, 'y' => 30 } ); sub stats { my %hhp = @_; my %counters; # a counter hash of which keys are used at level two foreach my $key1 (keys %hhp) { my @cList = (); foreach my $key2 (keys %{$hhp{$key1}}) { push(@cList,$key2); } ++$counters{join('+',sort @cList)}; } return %counters; } #### main my %result = stats(%hash); foreach my $key (keys %result){ print "$key $result{$key}\n"; }
The result looks like this:
x+y+z 1 y+z 1 x+z 1 x 1
To do statistics on the actual values.. use the same logic.
perlcapt
-ben