Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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?
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) { ..do something for a filled %hash... } else { ...do something for an empty %hash... }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2015-01-28 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My top resolution in 2015 is:

















    Results (213 votes), past polls