I think I got it. But I had to create a temp hash to build it.
use strict; use warnings; use Data::Dumper; my %in_hash = ( jibber => [ {id => 1, score => 3, name => 'foo'}, {id => 5, score => 1, name => 'bar'}, {id => 22, score => 6, name => 'baz'}, ], jabber => [ {id => 3, score => 1, name => 'boo'}, {id => 5, score => 3, name => 'bar'}, {id => 12, score => 2, name => 'zib'}, {id => 22, score => 2, name => 'baz'}, ], ); my %tmp; for my $key (keys %in_hash) { for my $href (@{$in_hash{$key}}) { my $id = $href->{id}; if (exists($tmp{$id})) { $tmp{$id}->{score} += $href->{score}; } else { $tmp{$id} = $href; } $tmp{$id}->{keys}->{$key}++; } } my %out_hash; for my $id (keys %tmp) { my $keys = $tmp{$id}->{keys}; delete $tmp{$id}->{keys}; my $new_key = join(' ', reverse sort keys %$keys); push(@{$out_hash{$new_key}}, $tmp{$id}); } print Dumper \%out_hash;
output...
C:\tmp>erick.pl $VAR1 = { 'jibber' => [ { 'name' => 'foo', 'score' => 3, 'id' => 1 } ], 'jabber' => [ { 'name' => 'boo', 'score' => 1, 'id' => 3 }, { 'name' => 'zib', 'score' => 2, 'id' => 12 } ], 'jibber jabber' => [ { 'name' => 'baz', 'score' => 8, 'id' => 22 }, { 'name' => 'bar', 'score' => 4, 'id' => 5 } ] };

In reply to Re: combining hashes based on key values by mifflin
in thread combining hashes based on key values by punkish

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.