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):

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 }
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.

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
    my %h1 = qw( a 1 b 2 c 3 ); my %h2 = qw( b 7 c 8 d 4 ); my %h3 = map { $_ => $h1{$_} // 0 + $h2{$_} // 0 } keys %h1, keys %h2;

    No warnings, merges both hashes


    Enjoy, Have FUN! H.Merijn

      The expression  $h1{$_} // 0 + $h2{$_} // 0 has a complicated precedence:

      c:\@Work\Perl\monks>perl -wMstrict -MO=Deparse,-p -le "my %h1 = qw( a 1 b 2 c 3 ); my %h2 = qw( b 7 c 8 d 4 ); my %h3 = map { $_ => $h1{$_} // 0 + $h2{$_} // 0 } keys %h1, keys %h2 +; " BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict 'refs'; (my(%h1) = ('a', '1', 'b', '2', 'c', '3')); (my(%h2) = ('b', '7', 'c', '8', 'd', '4')); (my(%h3) = map({($_, (($h1{$_} // (0 + $h2{$_})) // 0));} keys(%h1), k +eys(%h2))); -e syntax OK
      Are you sure it works in all circumstances? Wouldn't it be better to throw in a couple pair disambiguating parentheses? E.g.:
          ($h1{$_} // 0) + ($h2{$_} // 0)


      Give a man a fish:  <%-{-{-{-<