Keep It Simple, Stupid PerlMonks

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

by draco_iii (Acolyte)
 on Jul 05, 2000 at 08:34 UTC 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{\$_}; } [download]``` Editor's note: Or, as merlyn has pointed out, hash slices can make you very happy... ```@a{keys %b} = values %b; [download]``` 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 ); [download]``` 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}; [download]``` 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{\$_}; } [download]``` 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); [download]``` 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); [download]``` 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.

• 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.
• 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
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2018-09-21 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Eventually, "covfefe" will come to mean:

Results (185 votes). Check out past polls.

Notices?