Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: curious behavior: why does it do this?

by perl-diddler (Chaplain)
on Dec 10, 2019 at 01:33 UTC ( [id://11109891]=note: print w/replies, xml ) Need Help??


in reply to curious behavior: why does it do this?

One thing I noted that seems a bit perverse (or wrong). Is how I'm printing out the numbers 08 and 09, which under perl would fail as numbers. My print routine, P looks at what it is printing for list members and automatically quotes non-numeric entities, while not quoting numbers. So is it wrong to leave out those quotes around 08 and 09? I shouldn't feel too horrible, as Scalar::Util's function "looks_like_number" also think 08/09 are numbers -- as does perl itself:
> tperl use Scalar::Util qw(looks_like_number); for my $i ("00".."10") { P "%s(%d) %s like a number", "$i", "$i", looks_like_number($i) ? "look +s":"does not look"; }' 00(0) looks like a number 01(1) looks like a number 02(2) looks like a number 03(3) looks like a number 04(4) looks like a number 05(5) looks like a number 06(6) looks like a number 07(7) looks like a number 08(8) looks like a number 09(9) looks like a number 10(10) looks like a number
So nothing about invalid octal constants from perl. ... *Ouch*, on a whim, I tried inserting a 0 before the 10, thinking my list iteration _might_ stop at 8, I.e.:
for my $i ("00".."010") {...
I got an enumeration up through 999. Another unexpected result! Or is that really what others expected?

Perhaps as a nod to sanity, using 010 (with no quotes) as an end-value did end at 8, but the entire sequence was numified (with the leading 0 missing):

0(0) looks like a number 1(1) looks like a number 2(2) looks like a number 3(3) looks like a number 4(4) looks like a number 5(5) looks like a number 6(6) looks like a number 7(7) looks like a number 8(8) looks like a number

Replies are listed 'Best First'.
Re^2: curious behavior: why does it do this?
by haukex (Archbishop) on Dec 10, 2019 at 05:21 UTC
    So is it wrong to leave out those quotes around 08 and 09? I shouldn't feel too horrible, as Scalar::Util's function "looks_like_number" also think 08/09 are numbers

    As I explained here, literals in the code (Scalar value constructors) are different from what Perl's string-to-number conversion recognizes - 0+"08" is 8 but 08 is an illegal octal number (there is one exception; a bug that appeared in 5.30 but should be fixed by the next release). looks_like_number accesses the same internal function that Perl uses to tell if a string looks like a number.

    for my $i ("00".."010") {... I got an enumeration up through 999.

    I explained the rules for the range operator and magic string increment in detail here, the behavior you mention is consistent with those rules because "010" is not in the sequence produced by the magic string increment (only "10" is; compare that to "000".."010").

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2024-04-18 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found