Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Perl oddities

by jmcnamara (Monsignor)
on Mar 01, 2005 at 10:40 UTC ( #435351=note: print w/ replies, xml ) Need Help??


in reply to Perl oddities


I find the return value of a hash in a scalar context odd.

$ perl -le '%h = (a =>1, b => 2); print scalar %h' 2/8
From perldata:
If you evaluate a hash in scalar context ... the value returned is a string consisting of the number of used buckets and the number of allocated buckets ...

Buckets!

I can't imagine that this is useful to anyone except the implementor of the hashing function and the occasional person with pathological data.

It would be much more useful, or sensible, if hash in a scalar context returned the number of keys:

$ perl -le '%h = (a =>1, b => 2); print scalar keys %h' 2

And before anyone leaps to the defense of this artifact I'm just stating that I find it odd. I can live with it.

I think that there were a lot of things that I used to find odd but since I can't remember what they are I guess that I've just come to accept them. :-)

--
John.


Comment on Re: Perl oddities
Select or Download Code
Re^2: Perl oddities
by PreferredUserName (Pilgrim) on Mar 01, 2005 at 14:54 UTC
    I also find this artifact useless, but a saving grace is that you can at least say:
    if (%hash) { # it's not empty }
    even though the string "0/8" evaluates to true.
Re^2: Perl oddities
by fergal (Chaplain) on Mar 01, 2005 at 16:04 UTC
    Update: Nonsense below, I misread the post.

    It would be much more useful, or sensible, if hash in a scalar context returned the number of keys:
    As soon as you try to use it as a number, "2/8" will be numified to 2 so actually it does effectively return the number keys.
    perl -le '%h = (a =>1, b => 2); print %h+0' 2
    The number of buckets is some bonus info for free.

      In the above case yes but as the number of keys is increased the buckets will get reused and the result won't be correct:
      $ perl -le '%h = (1 .. 10); print scalar %h' 5/8 $ perl -le '%h = (1 .. 12); print scalar %h' 5/8 $ perl -le '%h = (1 .. 100); print scalar %h' 33/64
      :-)

      Actually, I searched for a genuine use for this feature for a long time and I almost found one: Power Twool.

      --
      John.

        Oops. I completely misread your original post. I thought it was elements/buckets, not used/total. That said, given Perl's non-object data model, the only other way to provide this info would be through yet another function.
Re^2: Perl oddities
by TimToady (Parson) on Mar 01, 2005 at 18:25 UTC
    In Perl 6, a hash in scalar context will return a reference to the hash, which will behave appropriately in boolean, numeric, or string contexts. (You'll have to call a special method to get the bucket filling information.) I'd like to note for the record, however, that the main reason hashes did not return the number of keys by default in numeric context in Perl 5 was that it was potentially a very expensive operation on DBM files and the like. But Perl 6 will gladly let you hang yourself by the foot there...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2014-09-20 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls