in reply to Counting hash of hashes elements

Here is a performance-improved version of perlcapt's solution - this one includes a response to your last query.
#!/usr/bin/perl -w use strict; use warnings; 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, $SearchKey, $SearchVal) = @_; my %counters; # a counter hash of which keys are used at level two my $SearchCount; foreach my $key1 (keys %$hhp) { my @cList = keys %{$hhp->{$key1}}; ++$counters{join('+',sort @cList)}; ++$SearchCount if exists $hhp->{$key1}->{$SearchKey} and $hhp->{$key1}->{$SearchKey} == $SearchVal +; } return $SearchCount, %counters; } #### main my ($SearchCount, %result) = stats(\%hash,'z',101); foreach my $key (sort keys %result){ print "$key \t$result{$key}\n"; } print "Count of z==101 is $SearchCount\n"; __END__ ## OUTPUT ## x 1 x+y+z 1 x+z 1 y+z 1 Count of z==101 is 1

    Earth first! (We'll rob the other planets later)

Replies are listed 'Best First'.
Re^2: Counting hash of hashes elements
by perlcapt (Pilgrim) on Nov 02, 2004 at 00:56 UTC
    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