Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How do I find out if a hash is empty?

by Pug (Monk)
on Jun 11, 2002 at 22:34 UTC ( [id://173677]=perlquestion: print w/replies, xml ) Need Help??

Pug has asked for the wisdom of the Perl Monks concerning the following question: (hashes)

How do I find out if a hash is empty?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I find out if a hash is empty?
by ysth (Canon) on Nov 04, 2003 at 09:56 UTC
    Preferably with  if (!keys %hash) { print "Empty" }.

    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.

    If tied hashes aren't a concern, either way will do, but there is a difference to be aware of: the keys version will reset the hash's iterator (see each) while the other way will leave it unchanged.

      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.

Re: How do I find out if a hash is empty?
by gbarr (Monk) on Jun 12, 2002 at 17:25 UTC
    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.

Re: How do I find out if a hash is empty?
by mojotoad (Monsignor) on Jun 12, 2002 at 05:14 UTC
    Beware the Ties of March!!!

    In my innagural post I pointed out that a hash in scalar context will not behave as expected when tied, although it's tasty syntax: if (%my_hash) { ... do stuff ... }

    I feel that's a bug and should be addressed.

    As a bulletproof workaround, always use keys in scalar context thusly: if (scalar keys %my_hash) { ...do stuff... }

    Matt

Re: How do I find out if a hash is empty?
by particle (Vicar) on Jun 11, 2002 at 22:49 UTC
    a hash in scalar context returns (somewhat uselessly -- except for now,) the number of slots filled out of the number of slots allocated. so scalar %hash will equal 0 if no slots are allocated.

    print 'empty' unles scalar %hash;
Re: How do I find out if a hash is empty?
by Pug (Monk) on Jun 11, 2002 at 22:38 UTC
    This seems to work.. As to why? I don't know.
    my %t; if (!%t) { print "Empty"; } else { print "Not Empty\n"; }
Re: How do I find out if a hash is empty?
by Pug (Monk) on Jun 11, 2002 at 22:44 UTC
    This seems to work.. As to why? I don't know.
    my %t; if (!%t) { print "Empty"; } else { print "Not Empty\n"; }

    Originally posted as a Categorized Answer.

Re: How do I find out if a hash is empty?
by Anonymous Monk on Sep 14, 2004 at 15:06 UTC

    Hum... I have tried all of those approaches and none of them work.

    For example, the following script:

    ---- Start script
    use strict; my %hash = {}; print "\%hash=".%hash."\n"; print "scalar(\%hash)=".scalar(%hash)."\n"; print "length(\%hash)=".length(%hash)."\n"; %hash = undef; print "defined(\%hash)=".defined(%hash)."\n";
    --- End script

    Results in the following output:

    %hash=1/8 scalar(%hash)=1/8 length(%hash)=3 defined(%hash)=1

    Does any of you know what the problem is? I am using perl v5.8.3.

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://173677]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found