in reply to Re: Re: Arrays: Last-index special variable vs. scalar values
in thread Arrays: Last-index special variable vs. scalar values

Says tenatious:
>It seems like if you evaluate a hash in scalar context, >i.e $x = (%hash); you get some funky fraction that doesn't mean much.
It's in the manual what it means. (Check perldata.) Probably the most useful thing about it is that it is false if the hash is empty and true if the hash is not empty, so you can use this:

if (%hash) { # it is not empty }
to test for an empty hash.

But yes, the fraction is information about the internal structure of the hash, and not usually useful. It tells you how many of the hash structure's internal 'buckets' have been used. 11/32 means that there are 32 buckets, and 11 of the buckets have some data in them.

Low-valued fractions mean that Perl's hashing algorithm is performing badly and the hash will have slower access. What you really want here is for the numerator of the fraction to be exactly equal to the number of keys in the hash. When this happens, every key has its own bucket and looking up keys is fast. When many keys are in the same bucket, Perl has to spend time searching through all the keys in the bucket looking for the one you want.

I have a program somewhere that exercises the worst-case behavior for Perl's hashes: It constructs a hash that has 10,000 keys, all of which are in the same bucket. Accessing this hash is very slow! The program looks at the funny fraction returned by %h in scalar context to check to make sure it is going the right thing. If the numerator of the fraction ever climbs above 1, the program knows that something has gone wrong!