in reply to Re: merge two hashes into one
in thread merge two hashes into one
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: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: merge two hashes into one
by Tux (Canon) on May 30, 2018 at 11:24 UTC | |
by AnomalousMonk (Archbishop) on May 30, 2018 at 13:59 UTC |