Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Quick Answer (TL;DR)</h2> <pre> %hash1 = (%hash1, %hash2) ## or else ... @hash1{keys %hash2} = values %hash2; ## or with references ... $hash_ref1 = { %$hash_ref1, %$hash_ref2 }; </pre> <h2>Overview</h2> <ul> <li><strong>Context:</strong> Perl 5.x</li> <li><strong>Problem:</strong> The user wishes to merge two hashes<sup>1</sup> into a single variable </li> </ul> <h2>Solution</h2> <ul> <li>use the syntax above for simple variables</li> <li>use Hash::Merge for complex nested variables</li> </ul> <h2>Pitfalls</h2> <ul> <li>What do to when both hashes contain one or more duplicate keys <ul> <li>Should a key-value pair with an empty value ever overwrite a key-value pair with a non-empty value?</li> <li>What constitutes an empty vs non-empty value in the first place? (eg. <code>undef</code>, empty string, <code>false</code>, falsy ...)</li> <li>(Note: the original question rules out this scenario, but it is an important one to consider)</li> </ul></li> </ul> <h2>See also</h2> <ul> <li><a href="http://www.perlmonks.org/?node_id=524456" rel="noreferrer">PM post on merging hashes</a></li> <li><a href="http://www.perlmonks.org/?node_id=157715" rel="noreferrer">PM Categorical Q&amp;A hash union</a></li> <li><a href="http://pleac.sourceforge.net/pleac_perl/hashes.html" rel="noreferrer">Perl Cookbook 5.10. Merging Hashes</a></li> <li>websearch://perlfaq "merge two hashes"</li> <li>websearch://perl merge hash</li> <li><a href="https://metacpan.org/pod/Hash::Merge" rel="noreferrer">https://metacpan.org/pod/Hash::Merge</a></li> </ul> <hr> <h2>Footnotes</h2> <p><sup>1</sup> * (aka <a href="https://stackoverflow.com/questions/tagged/associative-array">associative-array</a>, aka <a href="https://stackoverflow.com/questions/tagged/dictionary">dictionary</a>)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload