http://qs321.pair.com?node_id=396518

TVSET has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

While writing a test suite for our intranet, I came across an unexpected result. The bug has been fixed, but I am still puzzled over the meaning of the results. If scalar is given a hash as an argument, it returns something that looks like "2/8". For different hashes the result might be the same and I haven't noticed any trend in changes. The only guess I can make is the number of used/allocated buckets in the hash. Anyone has any idea and can clear things up for me?

Thanks in advance.

  • Comment on How should I understand scalar(%hash) results?

Replies are listed 'Best First'.
Re: How should I understand scalar(%hash) results?
by BrowserUk (Patriarch) on Oct 05, 2004 at 08:45 UTC

    Good guess :) From perldata:

    ..., but evaluating %HASH in scalar context reveals "1/16", which means only one out of sixteen buckets has been touched, ...

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
        In normal programs the only interesting thing about this is that it's guaranteed to return false for an empty hash and something that's true otherwise. So you can check if a hash is empty or not by simply using the hash name in boolean context (which is a scalar context):
        if (%hash) { ..do something for a filled %hash... } else { ...do something for an empty %hash... }