Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Answer: How do I find out if a hash is empty?

by gbarr (Monk)
on Jun 12, 2002 at 17:25 UTC ( #173938=categorized answer: print w/replies, xml ) Need Help??

Q&A > hashes > How do I find out if a hash is empty? - Answer contributed by gbarr

In a scalar context a hash yields a string like "1/4"

This is the number of buckets in use and the total number of buckets in the hash. When used in a numeric context it will result in the first number.

If the hash is empty, perl will just give "0"

But to prevent this generation of a string and then conversion to a number you can do

scalar keys %hash

You say beware ties. Well with ties everything goes out the window as there is no knowing if the tied package will behave in a usual fashion, ie it could arrange for keys to always return an empty list, but still return values for accesses. MJD has a package Interpolation that does this.

Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2020-12-03 10:40 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (55 votes). Check out past polls.