Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

How should I understand scalar(%hash) results?

by TVSET (Chaplain)
on Oct 05, 2004 at 08:39 UTC ( #396518=perlquestion: print w/ replies, xml ) Need Help??
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 (Pope) 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) { something for a filled %hash... } else { something for an empty %hash... }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://396518]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2016-05-29 01:29 GMT
Find Nodes?
    Voting Booth?