Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

( #21080=categorized question: print w/ replies, xml ) Need Help??
Contributed by draco_iii on Jul 05, 2000 at 08:34 UTC
Q&A  > hashes


Answer: How do I make all the values in %a with common keys in %b equal to %b's keys?
contributed by vroom

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;
Answer: How do I make all the values in %a with common keys in %b equal to %b's keys?
contributed by grinder

Hash slices may make you unhappy. The above will introduce %b's keys into %a regardless of whether they existed in %a or not. Consider

my %a = ( A => 1, B => 2, C => 3 ); my %b = ( A => 100, D => 200 ); @a{keys %b} = values %b; print( "$_ => $a{$_}\n" ) foreach( sort keys %a );
Answer: How do I make all the values in %a with common keys in %b equal to %b's keys?
contributed by CharlesClarkson

my @common = grep exists $a{$_}, keys %b; @a{@common} = @b{@common};
Answer: How do I make all the values in %a with common keys in %b equal to %b's keys?
contributed by cLive ;-)

Assuming %b has no null/empty/zero values:

for (keys %a) { $a{$_} = $b{$_} || $a{$_}; }
Answer: How do I make all the values in %a with common keys in %b equal to %b's keys?
contributed by Roy Johnson

CharlesClarkson's can be written without the use of a temp or slices:

$a{$_} = $b{$_} for (grep exists $b{$_}, keys %a);

which also benchmarks a little better. For clarity, I like vroom's solution.

Note that if you know that one hash is much larger than the other, you will do better to iterate through the keys of the smaller hash and test exists against the larger.

Answer: How do I make all the values in %a with common keys in %b equal to %b's keys?
contributed by QM

How do I make all the values in %a with common keys in %b equal to %b's keys?
Shouldn't that last keys be changed to values? Either that, or everyone has answered the wrong question.

For the question asked, it seems this would work:

$a{$_} = $_ for (grep exists $b{$_}, keys %a);
Answer: How do I make all the values in %a with common keys in %b equal to %b's keys?
contributed by grinder

Continuing my answer... the example code will correctly update the key 'A', but it will also introduce the key 'D', which may not be what you expected, given the original question.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (7)
    As of 2014-09-18 10:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (110 votes), past polls