in reply to Re: How to combine two different hashes into single hash.
in thread How to combine two different hashes into single hash.

Thanks Rolf,

Firstly, I should convert hash of hashes into hash and then use join ?

my $VAR1 = {'Tim' => {'abc' => 7, 'def' => 7, 'ghi' => 0 }, 'Adam' => +{ 'abc' => 0, 'def' => 0, 'ghi' => 0 }, 'keas' => { 'abc' => 0, 'def' + => 0, 'ghi' => 0 }}; my %h1 = ('Tim' => {'abc' => 7, 'def' => 7, 'ghi' => 0 }, 'Adam' => { +'abc' => 0, 'def' => 0, 'ghi' => 0 }, 'keas' => { 'abc' => 0, 'def' = +> 0, 'ghi' => 0 });

Apologies if I wrongly understood.

Replies are listed 'Best First'.
Re^3: How to combine two different hashes into single hash.
by LanX (Saint) on Dec 29, 2019 at 15:51 UTC
    I couldn't tell if your input and output hashes are in form of references or not.

    The following is a variant for references:

    use strict; use warnings; use Data::Dump qw/pp dd/; my $h1 = { 'Tim' => { 'abc' => 7, 'def' => 7, 'ghi' => 0 }, 'Adam' => { 'abc' => 0, 'def' => 0, 'ghi' => 0 }, 'keas' => { 'abc' => 0, 'def' => 0, 'ghi' => 0 } }; my $h2 = { 'Dav' => { 'jkl' => 12, 'mno' => 34, 'pqr' => 45 }, 'Adam' => { 'jkl' => 7, 'mno' => 0, 'pqr' => 7 } }; my %joined_1st = (%$h1,%$h2); #pp \%joined_1st; my $joined_2nd = { map { $_ => { %{ $h1->{$_} // {} }, %{ $h2->{$_} // {} } } } keys %joined_1st }; pp $joined_2nd;

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice