Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2016-10-23 19:25 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (302 votes). Check out past polls.