XP is just a number PerlMonks

### How do I compare key from one hash with values from other?

by Zombie mug (Initiate)
 on Dec 06, 2001 at 15:55 UTC Need Help??
Contributed by Zombie mug on Dec 06, 2001 at 15:55 UTC
Q&A  > hashes

#### Description:

I have two hashes. The keys of %hash1 are values in %hash2. %hash2 can have multiple values for the same key. I want to find the missing set of values (that is, keys of %hash1) for each key in %hash2.

Example:

```my %hash1 = (
A => 5,
B => 7,
C => 2,
D => 8,
);
my %hash2 = (
1234 => "A,C",
4567 => "A,B,D",
5678 => "C",
);
[download]```

 Answer: How do I compare key from one hash with values from other?contributed by 2501 I am not quite sure I understood your question. Try this out..I think this kludge might be what you are looking for. ```use strict; my %hash1 = ( A => 5, B => 7, C => 2, D => 8, ); my %hash2 = ( 1234 => "A,C", 4567 => "A,B,D", 5678 => "C", ); my %hash3; foreach my \$key2 ( keys %hash2 ) { my %temphash = %hash1; foreach ( split /,/, \$hash2{\$key2} ) { delete \$temphash{\$_}; \$hash3{\$key2}= join ',', sort keys %temphash; } } foreach my \$key ( keys %hash3 ) { print "\$key : \$hash3{\$key}\n"; } [download]``` Answer: How do I compare key from one hash with values from other?contributed by aspect_khaliq This will print missing set of values for keys from Hash1. ```use strict; my %hash1 = ( A => 5, B => 7, C => 2, D => 8, ); my %hash2 = ( 1234 => "A,C", 4567 => "A,D", 5678 => "C", ); my @a = split ",", join ',', values %hash2; my \$found = 0; foreach my \$key ( keys %hash1 ) { \$found = 0; foreach my \$val ( @a ) { \$found = 1 if \$val eq \$key; } print "\$key => \$hash1{\$key}\n" if !\$found; } [download]``` Output: ```B => 7 [download]``` Answer: How do I compare key from one hash with values from other?contributed by jdporter Well, you said "for each hash2 key", not "for all hash2 keys", so my solution is a bit different from the previous ones. ```use strict; my %hash1 = ( A => 5, B => 7, C => 2, D => 8, ); my %hash2 = ( 1234 => "A,C", 4567 => "A,B,D", 5678 => "C", ); while ( my( \$key2, \$val2 ) = each %hash2 ) { my %keys1; @keys1{ split /,/, \$val2 } = (); my @missing = grep { not exists \$keys1{\$_} } keys %hash1; print "hash2 key '\$key2' is missing values '@missing'\n"; } [download]```

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!
• 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, 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?
• 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?
NodeReaper says "Shhhh! Be vewy vewy quiet, I'm hunting wumpus"

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2018-04-26 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (97 votes). Check out past polls.

Notices?