Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Checking if hash value exists without implicitly defining it

by Ratazong (Prior)
on Feb 14, 2013 at 15:52 UTC ( #1018749=note: print w/ replies, xml ) Need Help??


in reply to Checking if hash value exists without implicitly defining it

Hi Doctrin,

as others have pointed out, you are suffering from autovivification. In order to avoid autovivification in your specific case, you could also check each level of the hash for existence before dereferencing it (and therefore before autovivification is triggered). The code could look like this:

if (exists $Hash{$neededVal} && exists $Hash{$neededVal}{param1} && ex +ists $Hash{$neededVal}{param1}{key1}) { print "DEFINED\n"; }
It takes advantage of the fact that the && s inside the condition are evaluated from left to right, and the evaluation is aborted once a "false" occurs.

HTH, Rata


Comment on Re: Checking if hash value exists without implicitly defining it
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-04-23 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (556 votes), past polls