Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Answer: How to check whether a hash is empty (has no keys)?

by ambrus (Abbot)
on Sep 05, 2011 at 10:32 UTC ( #924183=categorized answer: print w/replies, xml ) Need Help??

Q&A > Data Structures > How to check whether a hash is empty (has no keys)? - Answer contributed by ambrus

Just use the name of the hash in scalar context, such as

if (%foo) { # %foo has at least one key } else { # %foo has no keys }

If you want to be very explicit, you can also write

if (!!%foo) { ... }
or
if (scalar %foo) { ... }
or
if (0+%foo) { ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-05-25 04:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?