rspence,
According to perldoc -f each, you should not add keys to a hash while you are iterating over it in an each loop.

It is also likely that you can get away with just one loop by using exists and/or defined. Since I couldn't figure out exactly what you wanted, I am providing the nested loop framework and leaving the reading of perldoc up to you to see if it can be shortened.

#!/usr/bin/perl -w use strict; my (%hash1, %hash2); # Defined elsewhere in the program for my $key_h1 (keys %hash1) { my $val_h1 = $hash1{$key_h1}; for my $key_h2 (keys %hash2) { my $val_h2 = $hash2{$key_h2} # You can now check $key_h1/2 & $val_h1/2 # And safely modify %hash1 or %hash2 } }
Cheers - L~R

Update: Abigaill is correct (as usual), but I was confused as to what rspence actually wanted.

Since the root node has been updated with clarifications, my post looks bad


In reply to Re: Merging 2 hashes by Limbic~Region
in thread Merging 2 hashes - updated again by rspence

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.