Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: combining hashes based on key values

by mifflin (Curate)
on Jun 08, 2005 at 04:31 UTC ( [id://464553]=note: print w/replies, xml ) Need Help??


in reply to combining hashes based on key values

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 } ] };

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://464553]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-18 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found