in reply to Re: Counting hash of hashes elements
in thread Counting hash of hashes elements

The direct generation of the $key2 list directly into @cList is a nice improvement. I just want to add one more version more in the spirit of general statistical collection rather than situation testing. In this version, you can see what I call structural flattening. (Not sure that is the most appropriate term for it, but hey, I'm JAPH.)

If the statistics are too detailed or at the wrong level, just modify the sprintf expression.

We have pretty much hammered this little problem to death.

sub stats { my %hhp = @_; my $keyCounter = {}; # a counter hash of which keys are used at le +vel two my $statCounter = {}; # a counter for the actual values foreach my $key1 (keys %hhp) { my @cList = (); foreach my $key2 (sort keys %{$hhp{$key1}}) { push(@cList,$key2); my $statKey = sprintf("%s->%s=%s",$key1,$key2,$hhp{$key1}{$ +key2}); ++ $$statCounter{$statKey}; # make statistical entry on val +ue } ++ $$keyCounter{join('+',@cList)}; # make statistical entry on + key2 } return ($keyCounter,$statCounter); } #### main my ($keyResult,$statResult) = stats(%hash); foreach my $hash ($keyResult,$statResult){ foreach my $key (sort keys %$hash){ print "$key $$hash{$key}\n"; } } __END__ ## the result x 1 x+y+z 1 x+z 1 y+z 1 a->x=40 1 a->z=102 1 b->x=10 1 b->y=20 1 b->z=100 1 c->x=50 1 d->y=30 1 d->z=101 1
perlcapt
-ben