Now, %master_hash is keyed by the set of unique ids from the AoH, and its values are the sums of the values for matching ids.my %master_hash; for my $anon_hash_ref ( @AoH ) { $master_hash{ $$anon_hash_ref{id} } += $$anon_hash_ref{value}; }
update (because you updated the question while I was making up the initial answer): assuming that "id_a" always relates to "value_x" and "id_b" to "value_y":
another update: (because the previous update was wrong): Since the "id_a" and "id_b" values in your AoH might "intersect", this will keep them distinct:for my $anonhash ( @AoH ) { $master_hash{ $$anonhash{id_a} } += $$anonhash{value_x}; $master_hash{ $$anonhash{id_b} } += $$anonhash{value_y}; }
FINAL UPDATE: (sheesh!) Okay, based on your later clarification about the problem, I'd still suggest building a single hash as output, but now it should be either a HoH or HoA (whatever you prefer):for my $anonhash ( @AoH ) { $master_hash{ "a".$$anonhash{id_a} } += $$anonhash{value_x}; $master_hash{ "b".$$anonhash{id_b} } += $$anonhash{value_y}; }
(Of course, you'll want to delete or comment out whichever pair of lines above you don't prefer.)for my $anonhash ( @AoH ) { my $newkey = join '_', 'a', $$anonhash{id_a}, 'b', $$anonhash{id_b +}; # one way (HoH): $master_hash{$newkey}{value_x} += $$anonhash{value_x}; $master_hash{$newkey}{value_y} += $$anonhash{value_y}; # another way (HoA): $master_hash{$newkey}[0] += $$anonhash{value_x}; $master_hash{$newkey}[1] += $$anonhash{value_y}; }
In reply to Re: Messing about in Arrays of Hashes
by graff
in thread Messing about in Arrays of Hashes
by nzgrover
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |