Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Counting hash of hashes elements

by perlcapt (Pilgrim)
on Nov 01, 2004 at 11:53 UTC ( #404283=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Counting hash of hashes elements
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-07-25 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls