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):
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.)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; } }
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:
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.my %total; foreach my $subhash (values %all_files) { foreach my $key (keys %$subhash) { $total{$key} += $subhash->{$key}; } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |