Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have two different hashes. One has three values per key (triplet), the other has two values per key (doublet). Both hashes are comprised of similar entries, e.g. where @A1 and @A2 may have duplicate matches in them.

I am trying to compare both hashes to find key-value-value pairings in the doublet that do not appear in the triplet. I have something like this so far, which checks for the presence of keys in the hash. However, I am not sure how to scale this up to check for all three values in %doublet in the %triplet and then print out any rows that are missing.

I hope that someone can advise!

my %triplet = map {($A1[$_] => [$B1[$_], $C1[$_], $D1[$_]]) } 0..$#tri +plet; my %doublet = map {($A2[$_] => [$D2[$_], $C2[$_]])}0..$#doublet; for (my $i=0; $i<@A2; $i++) { if (exists($triplet{$A2[$i]})) { print "$A2[$i]\n"; } else { } }

Replies are listed 'Best First'.
Re: comparing values in two hashes
by graff (Chancellor) on Feb 14, 2006 at 02:18 UTC
    In order to find keys in one hash that do not appear in another hash, you do something like this:
    for my $key ( keys %one_hash ) { next if ( exists( $other_hash{$key} )); # now do something with this $key, which exists only in %one_hash. +.. }
    I'm afraid I don't understand the part about "scaling this up to check for all three values in %doublet in the %triplet and then print out any rows that are missing." Print out rows from what? Missing from where? I though that elements in %doublet only contain two values each.

    Am I correct to assume that you only compare hash values in the cases where the hash keys match? (If you wanted to compare all hash values, or ones where the keys don't match, that would be a lot more comparisons.)

    If your sample code gives any hints, I would expect that you might want something like this:

    for my $key ( keys %doublet ) { if ( exists( $triplet{$key} )) { # need to check values next if ( $doublet{$key}[0] eq $triplet{$key}[2] and $doublet{$key}[1] eq $triplet{$key}[1] ); } # get here if $key does not exist in %triplet, # OR if $triplet{$key} is not equivalent to $doublet{$key} print "$key\n"; }
    (I guessed at that based on how you used the various arrays in building the two hashes, assuming that @C1 might be "equivalent" to @C2, and likewise for @D1, @D2.)
Re: comparing values in two hashes
by ayrnieu (Beadle) on Feb 13, 2006 at 22:36 UTC
    Please see if this subroutine makes sense for your problem:
    sub foo_non_subsets { my ($h1, $h2) = @_; my %r; for my $c (List::Compare->new([keys %$h1], [keys %$h2])) { $r{$_} = $h1->{$_} for $c->get_Lonly; for ($c->get_union) { unless (List::Compare->new($h1->{$_}, $h2->{$_})->is_LsubsetR) { $r{$_} = $h1->{$_}; } } } %r }