Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

( #129889=categorized question: print w/ replies, xml ) 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", );

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"; }
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; }

Output:

B => 7
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"; }

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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 exploiting the Monastery: (4)
    As of 2015-07-04 23:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (60 votes), past polls