in reply to Merging 2 hashes - updated again

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

Replies are listed 'Best First'.
Re: Merging 2 hashes
by Abigail-II (Bishop) on Oct 29, 2003 at 20:14 UTC
    According to perldoc -f each, you should not add keys to a hash while you are iterating over it in an each loop.

    That's true, but the OP wasn't adding keys to the hash. He was modifying values. And that's perfectly save as the iterator iterates over keys, not values.

    Abigail

      That's true, but the OP wasn't adding keys to the hash.

      And this brings up the question, why was the OP's code failing? Iterating over the inner hash is senseless, but it should work.

      -sauoq
      "My two cents aren't worth a dime.";