Strange Hash Output

S

sso

I'm sure this makes sense somehow, just not to me as I am
inexperienced.

When I print my hash via:

print %myhash;

it prints out something like 2/8 or 4/8. Always a fraction like
that. Is there a general cause of this?
 
A

A. Sinan Unur

I'm sure this makes sense somehow, just not to me as I am
inexperienced.

When I print my hash via:

print %myhash;

it prints out something like 2/8 or 4/8.

Nope. That cannot happen. Printing the %hash in list context results in
the key-value pairs being printed.
Always a fraction like that. Is there a general cause of this?

You get the m/n notation when you evaluate a hash in scalar context.
That means m buckets out of the n are used. This is all documented in
perldoc perldata:

If you evaluate a hash in scalar context, it returns false if the hash
is empty. If there are any key/value pairs, it returns true; more
precisely, 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 internal
hashing algorithm is performing poorly on your data set.

You should, at the very least, read perldata, perlsyn, perlop, perlfunc
and, of course, perlfaq if you plan to learn Perl.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
U

Uri Guttman

CM> Yes. The cause is called, "that's how it's supposed to work." When you
CM> use a hash in a scalar context, you get a string indicating how many
CM> buckets out of the total available buckets the hash is using.

but the code above puts the hash in list context (from print). this
likely means the above code isn't what the OP actually ran.

perl -le '%h = ( a => 2 ) ; print %h'
a2

so the OP needs to post his real code and not what he thinks he ran.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,213
Messages
2,571,108
Members
47,700
Latest member
Naveed baloch

Latest Threads

Top