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

Re: Checking a hash for two matching values.

by moritz (Cardinal)
on Sep 17, 2013 at 04:34 UTC ( #1054384=note: print w/ replies, xml ) Need Help??


in reply to Checking a hash for two matching values.

The regex only works if the hash contains cat and dog in the same value, but judging from the description, that's not what you want.

Instead of trying to be too clever, I'd use something like this:

use 5.014; use warnings; my @hashes = ( { bla => 'cat', blubb => 'dog' }, { only => 'cat' }, { just => 'dog' }, ); sub hr_contains_cat_and_dog { my $hr = shift; my ($has_cat, $has_dog); for (values %$hr) { $has_cat = 1 if /cat/i; $has_dog = 1 if /dog/i; } return $has_cat && $has_dog; } my @result = grep hr_contains_cat_and_dog($_), @hashes; use Data::Dumper; print Dumper \@result; __END__ $VAR1 = [ { 'bla' => 'cat', 'blubb' => 'dog' } ];


Comment on Re: Checking a hash for two matching values.
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (17)
As of 2014-10-23 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (126 votes), past polls