Ah, this makes much more sense than the multilevel-hash question we saw the other day. The data here have obvious meaning. (Well, the keys do. I still don't know what the numbers are, but you said you want to sum them, so that's a clear objective.)

Your question has one thing in common with that one, though: we don't know ahead of time how deep the structure will go, and it can go deeper in some places than others. So you want to use a recursive solution, checking at each step whether what you've got is an array reference or a hash reference or what. Something like the solution I gave in that thread, only you want to sum the values instead of just printing them. Something along these lines:

my %country= ( 'United States' => { Ohio => { 'Franklin County' => { Columbus => [1, 3.14159, 2.71828], Delaware => [2, 3, 5, 7, 11, 13], }, 'Crawford County' => { Galion => [419, 468, 1708], }, 'Summit County' => [1.59, 2.78, 3.62], }, Iowa => [1.9999, 3], }, 'Jamaica' => [ 4.512, 1.8, 3], ); recursivelyprintsum(0, World => \%country); sub recursivelyprintsum { my ($level, $label, $data) = @_; if (ref $data eq 'ARRAY') { my $value; $value += $_ for @$data; print "\t" x $level; print "$label:\t$value\n"; return $value; } elsif (ref $data eq 'HASH') { print "\t" x $level; print "$label:\n"; my $subtotal; foreach my $unit (keys %$data) { $subtotal += recursivelyprintsum($level + 1, $unit, $$data{$unit +}); } print "\t" x $level; print "$label total:\t$subtotal\n"; return $subtotal; } else { # $data must actually be $datum. print "\t" x $level; print "$label:\t$data\n"; return $data; } }

Please note that if this is a homework assignment, and you turn in my code as it stands without understanding it, the prof will know you didn't write it yourself, because I've done some things a beginning programmer would not think to do (not in terms of the overall flow of how it works, but in the details). You should use my code as an example, try to understand how it works, and then try to make your code do the same thing. If it's for work, rather than school, then you can copy my code (your boss is unlikely to care), but you still should try to understand it.


Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. You can just call me "Mister Sanity". Why, I've got so much sanity it's driving me crazy.

In reply to Re: Processing Multilevel Hash by jonadab
in thread Processing Multilevel Hash by beginr

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.