in reply to How do I make all the values in %a with common keys in %b equal to %b's values?

Iterate through each key in %a. If %b contains the current hash key, set %a's value to %b's value for that key.
foreach(keys %a){ $a{$_} = $b{$_} if exists $b{$_}; }
Editor's note:
Or, as merlyn has pointed out, hash slices can make you very happy...
@a{keys %b} = values %b;

Replies are listed 'Best First'.
(dkubb) Re: (2) Answer: How do I make all the values in %a with common keys in %b equal to %b's keys?
by dkubb (Deacon) on Mar 30, 2001 at 13:22 UTC

    The second answer may be incorrect. According to the question: we are to find all the matches between the keys in %a and %b, and if there is a match, assign the value of the matching element from %b into the corresponding element in %a.

    A hash slice won't work in this case, consider:

    my %a = ( a => 1, b => 2, c => 3, ); my %b = ( b => 4, d => 8, f => 12, ); @a{ keys %b } = values %b;

    Now, %a is:

    %a = ( a => 1, b => 4, c => 3, d => 8, f => 12, );

    Which is the incorrect answer, according to the question. If we use a hash slice, we'll be copying all the elements from %b into %a, regardless if there are any matches, or not.