in reply to merging two hashes and updating the 1st hash keys.
I think this is what you want:
# cat 791596.pl use strict; use warnings; use Data::Dumper; my %h1 = ( e=>'f', c=>'d', a=>'b' ); my %h2 = ( e=>'K', a=>'J', q=>'V' ); do { my @b = grep {exists $h1{$_}} keys %h2; @h1{@b} = @h2{@b}; }; print Dumper(\%h1); # perl 791596.pl $VAR1 = { 'e' => 'K', 'c' => 'd', 'a' => 'J' };
The first hash is updated with the values from the second, but no new keys (such as q in the second hash) are added to the first hash.
...roboticusUpdate: Correction as per Gramps' reply. ;^)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: merging two hashes and updating the 1st hash keys.
by GrandFather (Saint) on Aug 27, 2009 at 20:52 UTC |