Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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 exploiting the Monastery: (6)
As of 2019-02-21 09:27 GMT
Find Nodes?
    Voting Booth?
    I use postfix dereferencing ...

    Results (109 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!