punkish has asked for the wisdom of the Perl Monks concerning the following question:
I have
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'}, ], );
and I want
my %out_hash = ( 'jibber jabber' => [ {id => 5, score => 4, name => 'bar'}, {id => 22, score => 8, name => 'baz'}, ], 'jibber' => [ {id => 1, score => 3, name => 'foo'}, ], 'jabber' => [ {id => 3, score => 1, name => 'boo'}, {id => 12, score => 2, name => 'zib'}, ], )
The %out_hash combines the entries in the incoming so that keys with common ids get their scores summed up, and other keys are left with the non-common id entries.
Monks that might ask what I have done thus far -- to them I spake, nothing more than constructing the above example hashes. Essentially, I am drawing a blank (other than doing some terrible, long-drawn, brute force method the very thought of which numbs me).
Update: Corrected the hash defs as per the most fascinating tlm
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: combining hashes based on key values
by davido (Cardinal) on Jun 08, 2005 at 04:00 UTC | |
by punkish (Priest) on Jun 08, 2005 at 04:09 UTC | |
by davido (Cardinal) on Jun 08, 2005 at 05:00 UTC | |
|
Re: combining hashes based on key values
by jdporter (Paladin) on Jun 08, 2005 at 04:15 UTC | |
|
Re: combining hashes based on key values
by tlm (Prior) on Jun 08, 2005 at 04:20 UTC | |
|
Re: combining hashes based on key values
by mifflin (Curate) on Jun 08, 2005 at 04:31 UTC |