Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

print scalar %hash... erm

by pileswasp (Monk)
on May 24, 2001 at 19:48 UTC ( [id://82966]=perlquestion: print w/replies, xml ) Need Help??

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

I was just playing around - as you do - (I make my own jam as well) and I've come across something I hadn't noticed before.

If you do: perl -e 'print scalar @INC' (or any other array) you get the number of elements in the array. Fair enough.

However, if you do: perl -e 'print scalar %ENV' (or any other hash) you get something along the lines of 42/64.

I'm confused. Can anyone tell me what this means?

It's not the number of keys/something 'cause perl -e 'print scalar keys %ENV' gives me 53 on the same %ENV.

Replies are listed 'Best First'.
Re: print scalar %hash... erm
by davorg (Chancellor) on May 24, 2001 at 19:56 UTC

    From perldoc perldata:

    If you evaluate a hash in a scalar context, it returns a value that is true if and only if the hash contains any key/value pairs. (If there are any key/value pairs, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash. This is pretty much useful only to find out whether Perl's (compiled in) hashing algorithm is performing poorly on your data set. For example, you stick 10,000 things in a hash, but evaluating %HASH in scalar context reveals "1/16", which means only one out of sixteen buckets has been touched, and presumably contains all 10,000 of your items. This isn't supposed to happen.)

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Dominus has an example program that makes that which isn't supposed to happen happen. Try it, you'll be truly appalled.
        Sheesh. It's not exactly a fair test though. The second hash population has all those calls to &randomstring and &hashval in it as well as the dodgy bucket bits....

        I'll give it a play. Cheers!

        Update:
        Doh. But it does have the if (time() - $lasttime  > 5) test in it.

        I'm going to lay in the park and drink beer. Have fun.

      Cheers, with the use of the keyword 'bucket' I've come across the stuff in perldoc perldebguts, but I'm still not totally unconfused.

      If there are 53 key/value pairs, why are there 64 buckets and why did only 42 of them get used?

      Do buckets have any use whatsoever to the average perl programmer or should I drink less coffee and stop asking questions at this time on a hot Thursday afternoon?

        Here's how a hash works, in a nutshell... You've got a list of buckets, each of which is a list of zero or more items. You also have a hash function that converts a key into a "hash" value used to look up a bucket. (For any given key, the hash function must always return the same result; otherwise you wouldn't be able to find your items again!)

        When you want to do something with a specific key, you call the hash function for that key, use the result to find the appropriate bucket, and then search all the items in that bucket for the one you want. So, instead of having to check against every item in the hash, you only have to check against a few items that happen to be in the same bucket.

        As you keep adding to the hash, each bucket has to hold more items. As the buckets get bigger, the lookups get slower, because you have to search through more items. That problem is solved by dynamically increasing the number of buckets and redistributing the items as the hash grows in size.

        Your hash has 64 buckets (Perl always uses a power of 2), although only 42 are being used to store the 53 items. That just means that, with that combination of hash function, number of buckets, and keys, a few of the keys are sharing buckets.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-24 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found