It sounds like what you really want to do is go from an array of hashes into a single hash:
my %master_hash; for my $anon_hash_ref ( @AoH ) { $master_hash{ $$anon_hash_ref{id} } += $$anon_hash_ref{value}; }
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.

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":

for my $anonhash ( @AoH ) { $master_hash{ $$anonhash{id_a} } += $$anonhash{value_x}; $master_hash{ $$anonhash{id_b} } += $$anonhash{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{ "a".$$anonhash{id_a} } += $$anonhash{value_x}; $master_hash{ "b".$$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 ) { 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}; }
(Of course, you'll want to delete or comment out whichever pair of lines above you don't prefer.)

In reply to Re: Messing about in Arrays of Hashes by graff
in thread Messing about in Arrays of Hashes by nzgrover

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.