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?

( #173677=categorized question: print w/ replies, xml ) Need Help??
Contributed by Pug on Jun 11, 2002 at 22:34 UTC
Q&A  > hashes


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

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.

Answer: How do I find out if a hash is empty?
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.

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

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

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

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;
Answer: How do I find out if a hash is empty?
contributed by Pug

This seems to work.. As to why? I don't know.

my %t; if (!%t) { print "Empty"; } else { print "Not Empty\n"; }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • 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 examining the Monastery: (10)
    As of 2014-09-17 18:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (94 votes), past polls