Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Check if hash key value pair is defined

by davido (Archbishop)
on Nov 15, 2012 at 07:10 UTC ( #1003943=note: print w/replies, xml ) Need Help??

in reply to Check if hash key value pair is defined

Keys in hashes are unique. The only way for a key to have multiple values is if the "value" is a reference to some container (an anonymous hash, anonymous array, or some object). Let's say your hash holds references to arrays. You might do this:

my %hash = ( unwanted => [1], wanted => [2, 3, 4] ); my $target_key = 'wanted'; if( exists $hash{ $target_key } && ref $hash{ $target_key } eq 'ARRAY' && @{ $hash{ $target_key } } > 1 ) { print "The element associated with $target_key holds a reference t +o an array of @{$hash{$target_key}} values.\n"; }


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2016-10-24 16:07 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (309 votes). Check out past polls.