in reply to HoH and accumulation

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...

Replies are listed 'Best First'.
Re: Re: HoH and accumulation
by djbiv (Scribe) on Jan 28, 2004 at 22:17 UTC
    thank you for your post! however trying to implement your second solution I am failing to see how this is adding the common keys within my HoH? I am using strict and warnings, the code above was just a snip from the entire program. Here is the code I threw in and played with.
    my %total; my %subhash; foreach my $subhash (values %all_files) { foreach my $key (keys %$subhash) { my $junk = $total{$key} + $subhash{$key}; print "$junk\n"; } }
    the results of adding this to total the values for the common keys always results in '0'? I have also tried creating a AoH instead but still have problems getting the correct accumalation... Here is the code for that.
    for my $i ( 0 .. $#AoH ) { for my $role (sort keys %{ $AoH[$i] } ) { my $tcount = $AoH[$i]{$role} + $AoH[$i]{$role}; print "$role=$tcount\n"; } }
    either way what I would like to accomplish is for each common key within the hash (either below the main array or main hash) accumalate the value for common keys...? maybe I'm just brain dead at this point from looking at it too long... thanks...
      I'm sorry, that was due to a typo on my part (I did warn that the code was untested).

      You need to write $subhash->{$key} and not $subhash{$key} because you need to dereference the scalar $subhash, not access %subhash. (As I note in my update, strict.pm catches this kind of bug for you.)

      If this fix confuses you, then I'd highly recommend reading references quick reference.