Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: $array[ 'Infinity' ]

by quester (Vicar)
on Dec 17, 2007 at 00:25 UTC ( #657350=note: print w/ replies, xml ) Need Help??


in reply to Re: $array[ 'Infinity' ]
in thread $array[ 'Infinity' ]

Using perl -MO=Deparse may shed some light on some of this weirdness, although I have no idea why on Earth anyone would implement 'inf' the way it works on my box.

perl -MO=Deparse -e 'print $n[inf]'
prints
print $n[9**9**9];
as does
perl -MO=Deparse -e 'print $n["inf"]'

 

On the other hand,

perl -MO=Deparse -e 'print $n[-inf]'
prints
print $n[-'inf'];

 

perl -MO=Deparse -e 'print "", inf==0 ? "a" : "b", 'inf'==0 ? "c" : "d +", -inf==0 ? "e" : "f"'
prints
print '', 'inf' == 0 ? 'a' : 'b', 'inf' == 0 ? 'c' : 'd', -'inf' == 0 +? 'e' : 'f';

As nearly as I can tell at the moment, inf is only equivalent 9**9**9 if it is used by itself as a subscript; it is taken as a bareword in a simple assignment, so

perl -MO=Deparse -e '$n[inf], $n[1+inf], $n[-inf] = inf'
prints
$n[9**9**9], $n[1 + 'inf'], $n[-'inf'] = 'inf';

 

My perl -v shows,

This is perl, v5.8.8 built for i486-linux-gnu-thread-multi

 

Does anyone know why? - quester


Comment on Re^2: $array[ 'Infinity' ]
Select or Download Code
Re^3: $array[ 'Infinity' ]
by educated_foo (Vicar) on Dec 17, 2007 at 01:36 UTC
    That is terrifyingly weird. But the 9**9**9 is just an artifact of B::Deparse, as can be seen here. I'm not sure why it does that.

    Then again, it makes a kind of twisted sense that $a[inf] means "last element of @a", and (completely by accident) $a[-inf] means "last element counting from the end."

Re^3: $array[ 'Infinity' ]
by ww (Bishop) on Dec 17, 2007 at 02:01 UTC
    Oddly (or maybe not so oddly) I get different output on a w2k box from the first cited by quester:
    perl -MO=Deparse -e "print $n[inf]" print $n[0]; -e syntax OK
    from...
    perl -v This is perl, v5.8.8 built for MSWin32-x86-multi-thread (with 33 registered patches, see perl -V for more detail) ... Binary build 819 [267479] provided by ActiveState http://www.ActiveSta +te.com Built Aug 29 2006 12:42:41
      It's been established that ActiveState's build doesn't support infinity.
Re: $array[ 'Infinity' ]
by benizi (Hermit) on Dec 19, 2007 at 23:55 UTC

    As to the "why", X = 9**9**9 == 9**(9**9) = 9**387420489. Nine to that power is a number aproximately 1.2 billion bits long. (log(9)/log(2) * (9**9)). So, for all intents and purposes, it's infinity. Since it's odd, -X will be negative infinity So, it avoids the (possible?) non-portability of a raw 'inf' or '-inf'.

    Update: Duh, Ben. Re: strikeout. Plus it's specifically negated. I was thinking (-9)**9**9)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2015-07-03 18:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (55 votes), past polls