in reply to How to combine two different hashes into single hash.
nope, you have a hash of hashes and want to join the second layer hashes with the same first layer keys.
joining hashes is easy %join = (%h1,%h2) (NB: it will eliminate duplicate keys from %h1 though).
what you need is to find the set of all 1st-keys to join the second layer hashes
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;
Questions?{ Adam => { abc => 0, def => 0, ghi => 0, jkl => 7, mno => 0, pqr => 7 + }, Dav => { jkl => 12, mno => 34, pqr => 45 }, keas => { abc => 0, def => 0, ghi => 0 }, Tim => { abc => 7, def => 7, ghi => 0 }, }
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
FootballPerl is like chess, only without the dice
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to combine two different hashes into single hash.
by Sami_R (Sexton) on Dec 29, 2019 at 13:27 UTC | |
by LanX (Saint) on Dec 29, 2019 at 15:51 UTC |