Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2018-11-18 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (205 votes). Check out past polls.

    Notices?