in reply to Re^2: curious behavior: why does it do this?
in thread curious behavior: why does it do this?

I'm puzzled (in an idle sort of way) as to why print for "1" .. "-1"; doesn't output the numbers 1 through to 99.

That is indeed quite strange, and I can't make sense of it at the moment... seems like a bug to me.

$ perl -wMstrict -MData::Dump -e' dd "0".."-1" ' (0 .. 99) $ perl -wMstrict -MData::Dump -e' dd "1".."-1" ' () $ perl -wMstrict -MData::Dump -e' dd "01".."-1" ' ("01", "02", "03", "04", "05", "06", "07", "08", "09", 10 .. 99) $ perl -wMstrict -MData::Dump -e' dd "90".."-1" ' () $ perl -wMstrict -MData::Dump -e' dd "1".."xx" ' (1 .. 99) $ perl -wMstrict -MData::Dump -e' dd "11".."xx" ' (11 .. 99) $ perl -wMstrict -MData::Dump -e' dd "90".."xx" ' (90 .. 99) $ perl -wMstrict -MData::Dump -e' dd "-1".."xx" ' -1 $ perl -wMstrict -MData::Dump -e' dd "0".." -1 " ' (0 .. 9999) $ perl -wMstrict -MData::Dump -e' dd " 0 ".." -1 " ' () $ perl -wMstrict -MData::Dump -e' dd " 11 ".." -1 " ' () $ perl -wMstrict -MData::Dump -e' dd "0.0".."-1.0" ' "0.0" $ perl -wMstrict -MData::Dump -e' dd " 0.0 ".." -1.0 " ' () $ perl -wMstrict -MData::Dump -e' dd "0.0".." 1.0 " ' "0.0" $ perl -wMstrict -MData::Dump -e' dd " 0.0 ".."1.0" ' (0, 1)

Maybe there's some kind of heuristic going wrong... for example, maybe the intention is that if both sides looks_like_number, then treat them as numbers instead of strings - but several of the cases above don't work like that. I dunno at the moment, this may need some digging in the sources. (I ran a bisect earlier, and it seems that at least the "1".."-1" case has always returned the empty list.)

Update: Reported as Bug #133695

Update 2019-08-27: See this node, which documents the behavior, as well as the change in the behavior as of Perl v5.32.

Replies are listed 'Best First'.
Re^4: curious behavior: why does it do this?
by roboticus (Chancellor) on Nov 28, 2018 at 18:18 UTC

    haukex:

    Hmmm ... I just played around with it using Devel::Peek to see what the values in the sequence are:

    #!env perl use strict; use warnings; use Devel::Peek; use Data::Dump 'pp'; use v5.20; say qq{"8" .. "C" generates strings "8" .. "9"}; dump_seq("8" .. "C"); say qq{"8" .. "9" generates integers 8 .. 9}; dump_seq("8" .. "9"); say qq{"-1" .. "2" generates integers -1 .. 2}; dump_seq("-1" .. "2"); say qq{"B" .. "C" generates strings "B" .. "C"}; dump_seq("B" .. "C"); say qq{"9" .. "7" generates nothing}; dump_seq("9" .. "7"); say qq{"C" to "A" generates strings "C" .. "Z"}; dump_seq("C" .. "A"); sub dump_seq { Dump($_) for @_; print "---\n\n"; }

    Running it gives (edited for brevity):

    $ perl pm_1226451.pm "8" .. "C" generates strings "8" .. "9" SV = PV(0x600004240) at 0x600003680 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x600069f20 "8"\0 CUR = 1 LEN = 10 SV = PV(0x600004260) at 0x600003758 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x600172e00 "9"\0 CUR = 1 LEN = 10 --- "8" .. "9" generates integers 8 .. 9 SV = IV(0x600003670) at 0x600003680 REFCNT = 2 FLAGS = (IOK,pIOK) IV = 8 SV = IV(0x600003748) at 0x600003758 REFCNT = 2 FLAGS = (IOK,pIOK) IV = 9 --- "-1" .. "2" generates integers -1 .. 2 SV = IV(0x600003670) at 0x600003680 REFCNT = 2 FLAGS = (IOK,pIOK) IV = -1 SV = IV(0x600003748) at 0x600003758 REFCNT = 2 FLAGS = (IOK,pIOK) IV = 0 SV = IV(0x600003760) at 0x600003770 REFCNT = 2 FLAGS = (IOK,pIOK) IV = 1 SV = IV(0x600003898) at 0x6000038a8 REFCNT = 2 FLAGS = (IOK,pIOK) IV = 2 --- "B" .. "C" generates strings "B" .. "C" SV = PV(0x600004240) at 0x600003680 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x6001b5be0 "B"\0 CUR = 1 LEN = 10 SV = PV(0x600004260) at 0x600003758 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x600069f20 "C"\0 CUR = 1 LEN = 10 --- "9" .. "7" generates nothing --- "C" to "A" generates strings "C" .. "Z" SV = PV(0x600004240) at 0x600003680 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x6001b5be0 "C"\0 CUR = 1 LEN = 10 SV = PV(0x600004260) at 0x600003758 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x600069f20 "D"\0 CUR = 1 LEN = 10 <<< SNIP SNIP SNIP >>> SV = PV(0x6001a99f0) at 0x6001a4160 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x600196010 "Z"\0 CUR = 1 LEN = 10 ---

    It appears that when both values appear to be integers, it terminates the loop before generating any values if the second value is less than the first. For strings, it appears to just keep on going, terminating when it hits "the last letter". I wonder how it interacts with the locale for non-ASCII/English character sets. (Not motivated, however, to do the experiments myself, nor knowledgeable enough about Unicode and non-English character sets to be sure I could do the experiments well had I wanted to.)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I just played around with it using Devel::Peek to see what the values in the sequence are

      Good idea - and checking with Devel::Peek::Dump() shows up another anomaly:
      C:\_32>perl -MDevel::Peek -le "@x=('0' .. '2'); Dump $x[0];" SV = PV(0x54c658) at 0x54bc28 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x548438 "0"\0 CUR = 1 LEN = 10 C:\_32>perl -MDevel::Peek -le "@x=('1' .. '2'); Dump $x[0];" SV = IV(0x31bc18) at 0x31bc28 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1
      It appears that something being done by the range operator changes the PV '1' to an IV, but leaves the string '0' as a PV.
      I wouldn't be surprised if this different treatment is causally related to the unexpected behaviour being exhibited.

      Cheers,
      Rob
Re^4: curious behavior: why does it do this?
by rsFalse (Chaplain) on Nov 29, 2018 at 00:10 UTC
    Maybe there's some kind of heuristic going wrong...

    Also, it looks like if left operand is not /^[a-zA-Z]*[0-9]*$/ and the right operand is shorter, then empty list (instead of the left operand) is returned (upd.except of number-like operands?).