Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: How to combine two different hashes into single hash.

by Sami_R (Sexton)
on Dec 29, 2019 at 13:27 UTC ( [id://11110724]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11110724]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found