Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: Perl oddities

by fergal (Chaplain)
on Mar 01, 2005 at 16:04 UTC ( #435497=note: print w/replies, xml ) Need Help??

in reply to Re: Perl oddities
in thread Perl oddities

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.

Replies are listed 'Best First'.
Re^3: Perl oddities
by jmcnamara (Monsignor) on Mar 01, 2005 at 16:26 UTC

    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.


      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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2020-10-24 15:58 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (246 votes). Check out past polls.