This has the drawback of actually going through fetching all the keys for a tied hash, but the alternative (if (!%hash) { print "Empty" }) doesn't work at all for tied hashes.
Actually, using !keys is over 6x faster than using !%hash, and it does not get slower with a larger hash, so it is pretty clear that Perl does not have to iterate through all the keys to get the key count. Another way to do it would be to say !each %hash (which, of course, advances the iterator on non-empty hashes). However, the keys method is about twice as fast as that.
In reply to Re: Answer: How do I find out if a hash is empty?
by Roy Johnson
in thread How do I find out if a hash is empty?
by Pug
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |