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?? |
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.)
In Section
Seekers of Perl Wisdom
|
|