Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^8: how to identify a null hash value

by aaron_baugher (Curate)
on Jun 05, 2015 at 01:34 UTC ( [id://1129146]=note: print w/replies, xml ) Need Help??


in reply to Re^7: how to identify a null hash value
in thread how to identify a null hash value

When a reference to a hash is evaluated in scalar context, as in your if statement, Perl stringifies it into something like HASH(0x80182c2b8), where the hexadecimal number is the memory location where the actual hash is stored. So by checking for /HASH\(/, you can see that it's a hash reference (or a string that looks like one), but that doesn't tell you whether the hash it points to has any keys and values. Your if(! test will only return true if the value for the 'QUESTION_TEXT' key isn't a hash reference at all. Try this to prove it:

$h = {}; # create reference to an empty hash print $h; # prints HASH(0xSOMETHING)

It sounds like what's happening in your case is that $hh{QUESTION_TEXT} sometimes holds a hash reference, and sometimes holds a string. You can check for that first with the ref function. That will tell you whether the value is a hash reference, and if it is, then you can dereference it as stevieb and I both showed, to see if it's empty.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-19 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found