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

Hi all,
I have a question to scalar and hash of hashes, I thought I could find the size of the "number" level like this. But the answers in DB<8> and DB<10> are suprising for me.
And DB<12> confuses me even more.
-> perl -de 1 Loading DB routines from perl5db.pl version 1.19 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 1 DB<1> $a{'a'}{0}=0 DB<2> print scalar %{$a{'a'}} 1/8 DB<3> $a{'a'}{1}=0 DB<4> print scalar %{$a{'a'}} 2/8 DB<5> $a{'a'}{2}=0 DB<6> print scalar %{$a{'a'}} 3/8 DB<7> $a{'a'}{3}=0 DB<8> print scalar %{$a{'a'}} 3/8 DB<9> $a{'a'}{4}=0 DB<10> print scalar %{$a{'a'}} 3/8 DB<11> $a{'a'}{5}=0 DB<12> print scalar %{$a{'a'}} 4/8
How could I get the size of this? Any hints?

Thanks Andreas

Replies are listed 'Best First'.
Re: Size of hash of hashes
by kvale (Monsignor) on Feb 26, 2004 at 23:49 UTC
    To get the number of hash elements, use the  keys function:
    $a{a}{$_}++ foreach (0..5); print scalar keys %{ $a{a} };

    -Mark

      Thanks
      Andreas
Re: Size of hash of hashes
by Roger (Parson) on Feb 26, 2004 at 23:59 UTC
    1/8 means 1 bucket used out of 8 allocated.

    for (0..15) { $a{'a'}{$_} = 0; print scalar %{$a{'a'}}, "\n"; }

    And the output is -
    1/8 2/8 3/8 3/8 3/8 4/8 5/8 6/8 8/16 9/16 10/16 11/16 11/16 11/16 12/16 15/32

    If you want to find out the actual size, flatten into a list first.
    print scalar @{[%{$a{'a'}}]}, "\n";