Just use this 2 functions, and see the example.
This accept multi-dimensional hash and array.

my %H = ( a => { aaa => 111 , bbb => 222 , } , b => { ccc => 333 , ddd => 444 , } , c => [a,b,c,d, {x=>555,y=>666} , [w,w,w] ] , ); my ($keys,$vals) = HASH_size(%H) ; print "Size of keys: $keys\nSize of values: $vals\n" ; ############# # HASH_SIZE # ############# sub HASH_size { my (%hash) = @_ ; my ($KEYS_size,$VALS_size) ; foreach my $Key ( keys %hash ) { if ( ref( $hash{$Key} ) =~ /HASH/i) { $KEYS_size += length($Key) ; my ($k,$v) = &HASH_size( %{$hash{$Key}} ) ; $KEYS_size += $k ; $VALS_size += $v ; } elsif ( ref( $hash{$Key} ) =~ /ARRAY/i) { $KEYS_size += length($Key) ; my ($k,$v) = &ARRAY_size( @{$hash{$Key}} ) ; $KEYS_size += $k ; $VALS_size += $v ; } else { $KEYS_size += length($Key) ; $VALS_size += length($hash{$Key}) ; } } return($KEYS_size,$VALS_size) ; } ############## # ARRAY_SIZE # ############## sub ARRAY_size { my ( @array ) = @_ ; my ($KEYS_size,$VALS_size) ; for(0..$#array) { if (ref(@array[$_]) =~ /ARRAY/i) { my ($k,$v) = &ARRAY_size( @{@array[$_]} ) ; $KEYS_size += $k ; $VALS_size += $v ; } elsif (ref(@array[$_]) =~ /HASH/i) { my ($k,$v) = &HASH_size( %{@array[$_]} ) ; $KEYS_size += $k ; $VALS_size += $v ; } else {$VALS_size += length(@array[$_]) ;} } return( $KEYS_size,$VALS_size ) ; }

"The creativity is the expression of the liberty".

In reply to Re: Function to measure size of a hash. (multi-dimensional example) by gmpassos
in thread Function to measure size of a hash by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.