in reply to Checking a hash for two matching values.

G'day walkingthecow,

Your posted solution seems overly complicated. Some test data would have been useful. Here's how I might have tackled this:

$ perl -Mstrict -Mwarnings -e ' use List::MoreUtils qw{uniq}; use Data::Dumper; my @pets = ( { a => "cat", b => "dog" }, { c => "dog", d => "cat" }, { e => "dog" }, { f => "cat" }, { g => "goldfish" }, { h => "cat", i => "cat" }, { j => "cat", k => "cat", l => "dog", m => "dog" }, ); my @cat_and_dog; for (@pets) { push @cat_and_dog, $_ if 2 == grep { /^(?:cat|dog)$/ } uniq va +lues %$_; } print Dumper \@cat_and_dog; ' $VAR1 = [ { 'b' => 'dog', 'a' => 'cat' }, { 'd' => 'cat', 'c' => 'dog' }, { 'j' => 'cat', 'k' => 'cat', 'l' => 'dog', 'm' => 'dog' } ];

-- Ken

Replies are listed 'Best First'.
Re^2: Checking a hash for two matching values.
by moritz (Cardinal) on Sep 17, 2013 at 05:54 UTC

      Yes, I had considered adding other test data: "cats", "dogs", "catalogue", "raining cats and dogs", etc. I wasn't really sure what walkingthecow wanted, which prompted the "Some test data would have been useful." comment.

      -- Ken