Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Finding the size of a nested hash in a HoH

by Jeri (Scribe)
on Nov 10, 2011 at 14:24 UTC ( [id://937384]=perlquestion: print w/replies, xml ) Need Help??

Jeri has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I'm trying to find the size of a nested hash in a HoH. Here i what I've written.

sub ResetHashProteinTally { my ($rHoH_ProteinFamilies) = @_; #capturing array for my $proteinfamily ( keys %{$rHoH_ProteinFamilies} ) { my $size = (keys %{$rHoH_ProteinFamilies->{proteinfamily}}); #--- +-> finds the length of a hash print "$size\n"; } } #end of sub routine ResetHashProteinTally

Here is the output I'm getting.

0 0 0

(There are only 3 nested hashes in the initial data set I'm working with.)

I'm still new to working with HoHes so I've been coming across a couple conceptual problems like this. Thanks in advance.

Replies are listed 'Best First'.
Re: Finding the size of a nested hash in a HoH
by moritz (Cardinal) on Nov 10, 2011 at 14:26 UTC
      Man. Thanks, (kinda embarrassing)
Re: Finding the size of a nested hash in a HoH
by mrstlee (Beadle) on Nov 10, 2011 at 16:11 UTC
    Maybe using 'each' is a more natural way to go through a hash:
    while( my($key, $val) = each %{$rHoH_ProteinFamilies}){ my $size = (keys %{$val->{proteinfamily});

    I suppose a more devious method to find the size of a hash is to use the equivalence between arrays and hashes:

    my $size = @{[%$val->{proteinfamily}]}/2;

    In other words you can think of hashes as arrays sliced up into sequential pairs, p1 & p2, where p1 = the key and p2 = the value. Hence the hash size will always be 1/2 the length of the array representation.

      I suppose a more devious method to find the size of a hash

      Why would you use a "devious method", when scalar keys %{ $hashref } gives you it directly, without 1) iterating the entire hash; 2) contructing an array to hold the keys and values of the hash; 3) throwing away all that work and memory having only extracted a count; which you then have to divide by 2 anyway?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        If each were removed from the language, would anyone really miss it? I know it'd break some legacy code, although I doubt I've used it more than once every few years. But is there ever a time when each saves more than a line of code over using keys and then getting the value in a second step? (Hmm, I'm off to search and see if there's already been a "what's the least useful core function" thread.)

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://937384]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found