map could also be used to generate the sums of the intersection set of the two hashes (with no exceptions thrown):
The exists test could be moved into a separate grep step for perhaps greater clarity, but I'd expect a bit of a performance hit with large hashes.c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my %h1 = qw(a 1 b 2 c 3 ); my %h2 = qw( b 7 c 8 d 4); ;; my %h_out = map exists $h2{$_} ? ($_ => $h1{$_} + $h2{$_}) : (), keys %h1 ; dd \%h_out; " { b => 9, c => 11 }
BTW: Won't
my %h3 = map {$_ => $h1{$_} + $h2{$_}} keys %h1, keys %h2;
cause "Use of uninitialized value ..." warnings unless that warning is disabled?
Give a man a fish: <%-{-{-{-<
In reply to Re^2: merge two hashes into one
by AnomalousMonk
in thread merge two hashes into one
by ovedpo15
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |