Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Counting hash of hashes elements

by perlcapt (Pilgrim)
on Nov 02, 2004 at 00:56 UTC ( #404478=note: print w/ replies, xml ) Need Help??


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


Comment on Re^2: Counting hash of hashes elements
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://404478]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2014-09-22 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (193 votes), past polls