First, style nits. In your open or die, have the filename in your error message. That will help debugging if anything goes wrong later. Similarly using strict.pm avoids a lot of subtle bugs, particularly when you start working with nested data structures. (A typo can mean that you are accessing a very different place than you think you are.) I can tell that you can't be doing that because you haven't done things like declare $current_tps_file with my.

With those nits out of the way, here are a few workable strategies.

The most general is to generate a hash, by key, of which subhashes have that key. For instance (all code is untested):

my %with_key; foreach my $first_key (keys %all_files) { foreach my $second key (keys %{$all_files{$first_key}}) { push @{$with_key{$second_key}}, $first_key; } }
That generates a hash of arrays. Now you use that to figure out whatever you want. (At the cost of possibly having to write lots of code.)

A second strategy is to say that it makes no sense to search the entire data structure to produce a complex new data structure that you have to search again. So if what you need is simple, then you could walk all of the data and generate the totals that you need in one pass:

my %total; foreach my $subhash (values %all_files) { foreach my $key (keys %$subhash) { $total{$key} += $subhash->{$key}; } }
In fact if you're going to do that, there is no reason not to move generating %total into the original pass through the file.

I don't know exactly what you want to do, but either of these strategies will work.

UPDATE: I added an arrow in the second code snippet to dereference $subhash. This is the kind of typo that strict.pm catches for me when I try to run code...


In reply to Re: HoH and accumulation by tilly
in thread HoH and accumulation by djbiv

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.