Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Checking a hash for two matching values.

by kcott (Abbot)
on Sep 17, 2013 at 04:39 UTC ( #1054386=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Checking a hash for two matching values.
Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054386]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-12-17 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (40 votes), past polls