Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by tobyink (Canon)
on Dec 28, 2019 at 23:17 UTC ( [id://11110714]=note: print w/replies, xml ) Need Help??


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

my $VAR1 = { 'Tim' => { 'abc' => 7, 'def' => 7, 'ghi' => 0 }, 'Adam' => { 'abc' => 0, 'def' => 0, 'ghi' => 0 }, 'keas' => { 'abc' => 0, 'def' => 0, 'ghi' => 0 } }; my $VAR2 = { 'Dav' => { 'jkl' => 12, 'mno' => 34, 'pqr' => 45 }, 'Adam' => { 'jkl' => 7, 'mno' => 0, 'pqr' => 7 } }; my %output; $output{$_} ||= {( %{$VAR1->{$_}}, %{$VAR2->{$_}} )} for keys(%$VAR1), keys(%$VAR2); use Data::Dumper; print Dumper \%output;

I'll explain how this all works.

for keys(%$VAR1), keys(%$VAR2) will loop through the keys of both hashes. Some keys are in both arrays, so they'll be visited on the loop twice, but we can worry about that later. The key is in the variable $_.

%{$VAR1->{$_}} and %{$VAR2->{$_}} get the nested hashes from $VAR1 and $VAR2 using the key $_

(... , ...) combines those two hashes into a single list of key-value pairs. And wrapping that in { ... } creates a new hash using those key-value pairs and returns a reference to it.

$output{$_} is the slot in the new hash %output to store the hashref created above, and assigning to it with ||= will skip all of the above stuff if $output{$_} is already true. (This skips duplicated keys. Though duplicated keys aren't actually a problem in this case; they would merely slow down the loop an almost immeasurably small amount.)

Replies are listed 'Best First'.
Re^2: How to combine two different hashes into single hash.
by LanX (Saint) on Dec 28, 2019 at 23:38 UTC
    Running your code I get "Can't use an undefined value as a HASH reference" (update: using warnings )

    ... and Perl exits "compilation exited abnormally with code 255 at Sun Dec 29 00:36:58" (update: using strict )

    That's why I had to add the // {} parts in my code to default to an empty hash.

    Did you tweak your Perl?

    my version: This is perl 5, version 24, subversion 1 (v5.24.1) built for MSWin32-x64-multi-thread

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

      It'll do that with use strict.

      You could do this:

      my %output; $output{$_} ||= {( %{$VAR1->{$_}||{}}, %{$VAR2->{$_}||{}} )} for keys(%$VAR1), keys(%$VAR2);

      Not really any need to use the // operator which limits backwards compatibility. (Introduced in Perl 5.10.)

Re^2: How to combine two different hashes into single hash.
by Sami_R (Sexton) on Dec 29, 2019 at 14:26 UTC
    Thanks Toby, Great detailed explanation.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-25 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found