Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"-4".."0" range not working #6040

Closed
p5pRT opened this issue Oct 28, 2002 · 11 comments
Closed

"-4".."0" range not working #6040

p5pRT opened this issue Oct 28, 2002 · 11 comments
Labels

Comments

@p5pRT
Copy link
Collaborator

@p5pRT p5pRT commented Oct 28, 2002

Migrated from rt.perl.org#18114 (status was 'resolved')

Searchable as RT18114$

@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Oct 28, 2002

From a.shankar@ti.com

Created by a.shankar@ti.com

This is a bug report for perl from a.shankar@​ti.com,
generated with the help of perlbug 1.33 running under perl v5.6.1.

-----------------------------------------------------------------
I wrote the following program​:

$w1=<STDIN>;
$w2=<STDIN>;
@​numlist = ($w1 .. $w2);
print @​numlist;

[run]
-4 (first input)
0 (second input)

it prints nothing!

Perl Info

Flags:
    category=core
    severity=medium

Site configuration information for perl v5.6.1:

Configured by ActiveState at Mon Jun 17 21:32:50 2002.

Summary of my perl5 (revision 5 version 6 subversion 1) configuration:
  Platform:
    osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread
    uname=''
    config_args='undef'
    hint=recommended, useposix=true, d_sigaction=undef
    usethreads=undef use5005threads=undef useithreads=define
usemultiplicity=define
    useperlio=undef d_sfio=undef uselargefiles=undef usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
  Compiler:
    cc='cl', ccflags
='-nologo -O1 -MD -DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT 
 -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DPERL_MSVCRT_READFIX',
    optimize='-O1 -MD -DNDEBUG',
    cppflags='-DWIN32'
    ccversion='', gccversion='', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=4
    alignbytes=8, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='link', ldflags
'-nologo -nodefaultlib -release  -libpath:"D:/Perl\lib\CORE"  -machine:x86'
    libpth="D:\Program Files\Microsoft.NET\FrameworkSDK\Lib\" "C:\Program
Files\Microsoft Visual Studio .NET\Vc7\lib\" "D:\Perl\lib\CORE"
    libs=  oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib
uuid.lib wsock32.lib mpr.lib winmm.lib  version.lib odbc32.lib odbccp32.lib
msvcrt.lib
    perllibs=  oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib
uuid.lib wsock32.lib mpr.lib winmm.lib  version.lib odbc32.lib odbccp32.lib
msvcrt.lib
    libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl56.lib
  Dynamic Linking:
    dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ',
ddlflags='-dll -nologo -nodefaultlib -release  -libpath:"D:/Perl\lib\CORE"  
-machine:x86'

Locally applied patches:
    ACTIVEPERL_LOCAL_PATCHES_ENTRY


@INC for perl v5.6.1:
    D:/Perl/lib
    D:/Perl/site/lib
    .


Environment for perl v5.6.1:
    HOME (unset)
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=D:\Perl\bin\;D:\Program
Files\Microsoft.NET\FrameworkSDK\Bin\;C:\Program Files\Microsoft Visual
Studio
....NET\Common7\IDE\;C:\WINNT\Microsoft.NET\Framework\v1.0.3705\;C:\Program
Files\Microsoft Visual Studio
....NET\Vc7\bin\;C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;d:\Program
Files\Rational\ClearQuest;C:\Program
Files\Rational\Clearcase\bin;;c:\abhishek
    PERL_BADLANG (unset)
    SHELL=D:\PROGRA~1\RATIONAL\RATION~1\NUTCROOT\mksnt\sh.exe

Abhishek Shankar
C6000 Catalog Apps Team
Texas Instruments India


@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Oct 29, 2002

From @tamias

On Mon, Oct 28, 2002 at 08​:18​:24AM -0000, a.shankar@​ti.com (via RT) wrote​:

I wrote the following program​:

$w1=<STDIN>;
$w2=<STDIN>;
@​numlist = ($w1 .. $w2);
print @​numlist;

[run]
-4 (first input)
0 (second input)

it prints nothing!

I see that you are running Perl on Windows. Try printing a blank line
before you print anything else​:

$w1=<STDIN>;
$w2=<STDIN>;
@​numlist = ($w1 .. $w2);
print "\n";
print @​numlist;

Some Windows shells seem to hide the first line of output.

Ronald

@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Oct 29, 2002

From a.shankar@ti.com

Hi Ronald

The print("\n"); is not solving the problem. In fact a new problem has come
up. Under Windows if any of the inputs is 0 the program fails. In Unix the
input -4 and 0 prints the required list of numbers. But the inputs 0 and 4
fail to print anything.

Abhishek Shankar
C6000 Catalog Apps Team
Texas Instruments India

-----Original Message-----
From​: rt@​x1.develooper.com [mailto​:rt@​x1.develooper.com]On Behalf Of Ronald
J Kimball (via RT)
Sent​: Tuesday, October 29, 2002 8​:49 AM
To​: a.shankar@​ti.com
Subject​: Re​: [perl #18114] [no subject]

On Mon, Oct 28, 2002 at 08​:18​:24AM -0000, a.shankar@​ti.com (via RT) wrote​:

I wrote the following program​:

$w1=<STDIN>;
$w2=<STDIN>;
@​numlist = ($w1 .. $w2);
print @​numlist;

[run]
-4 (first input)
0 (second input)

it prints nothing!

I see that you are running Perl on Windows. Try printing a blank line
before you print anything else​:

$w1=<STDIN>;
$w2=<STDIN>;
@​numlist = ($w1 .. $w2);
print "\n";
print @​numlist;

Some Windows shells seem to hide the first line of output.

Ronald

@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Oct 30, 2002

From @tamias

On Tue, Oct 29, 2002 at 08​:58​:49AM +0530, Abhishek Shankar wrote​:

Hi Ronald

The print("\n"); is not solving the problem. In fact a new problem has come
up. Under Windows if any of the inputs is 0 the program fails. In Unix the
input -4 and 0 prints the required list of numbers. But the inputs 0 and 4
fail to print anything.

I have confirmed this behavior under perl5.6.1 and perl5.8.0, with help
from Garry Williams. In perl5.005_03, the code produces the expected
result, -4-3-2-10

However, under perl5.6.1 and perl5.8.0, the range operator returns an empty
list.

The behavior seems to occur whenever both operands are strings​:

perl/bin/perl -wle 'print "-4\n".."0\n"'
perl/bin/perl -wle 'print "-4".."0"'

If either operand is a number, a list is returned.

perl -wle 'print "-4"..0'
perl -wle 'print -4.."0"'

Is this a bug, or an intended change in behavior?

Abhishek, you can get the behavior you want by explicitly converting an
operand to a number​:

perl -wle '$w1="-4"; $w2="0"; print 0+$w1..$w2'

Ronald

@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Oct 30, 2002

From @demerphq

J Kimball (via RT) on Tuesday, October 29, 2002 8​:49 AM
On Mon, Oct 28, 2002 at 08​:18​:24AM -0000, a.shankar@​ti.com
(via RT) wrote​:

I wrote the following program​:

$w1=<STDIN>;
$w2=<STDIN>;
@​numlist = ($w1 .. $w2);
print @​numlist;

[run]
-4 (first input)
0 (second input)

it prints nothing!

I see that you are running Perl on Windows. Try printing a blank line
before you print anything else​:

Im pretty sure that this is not the cause of the problem.

To which Abhishek Shankar said on 29 October 2002 04​:34

The print("\n"); is not solving the problem. In fact a new
problem has comes up. Under Windows if any of the inputs is 0 the program
fails. In Unix the input -4 and 0 prints the required list of numbers. But
the
inputs 0 and 4 fail to print anything.

I agree that this is a bug. It appears to be because the strings '0\n' and
'-4\n' are not being interpreted in numeric context in the .. operator.
(Although I can't duplicate your problem with "0".."4" under unix I suspect
the solution is the same, see below.)

At first I thought this was due to the documented behaviour of the ..
operator. In that "0\n" .. "-4\n" gets evaluated. Since neither have been
used in numeric context they are treated as strings. Since they dont match
the rules for string increment nor for numeric arguments the range operator
returns an empty list. Unfortunatley this explanation doesn't wash​:

  E​:\Bin>perl -e "print qq{-4\n}..qq{-0\n}"
  -4-3-2-10
  E​:\Bin>perl -e "print 0+qq{-4\n}..0+qq{0\n}"
  -4-3-2-10

Which is more or less DWIM i think. But this sure isnt​:

  E​:\Bin>perl -e "print qq{-4\n}..qq{0\n}"

  E​:\Bin>

So, my vote is that this is a bug. (it works with ("-4\n".."-0\n") but not
("-4\n".."0\n") !!?) Oh, and stripping the "\n" out doesnt change the
results.

  V​:\perl\lib\CPAN>perl -e "print qq{-4}..qq{0}"

  V​:\perl\lib\CPAN>perl -e "print 0+qq{-4}..0+qq{0}"
  -4-3-2-10

Luckily the workaround is trivial indeed. Simply coerce numeric context.

  @​numlist = (0+$w1 .. 0+$w2);

or

  $w1=0+<STDIN>;
  $w2=0+<STDIN>;

HTH.

Yves
ps

Summary of my perl5 (revision 5 version 6 subversion 1) configuration​:
  Platform​:
  osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread
  uname=''
  config_args='undef'
  hint=recommended, useposix=true, d_sigaction=undef
  usethreads=undef use5005threads=undef useithreads=define
usemultiplicity=define
  useperlio=undef d_sfio=undef uselargefiles=undef usesocks=undef
  use64bitint=undef use64bitall=undef uselongdouble=undef
  Compiler​:
  cc='cl', ccflags ='-nologo -O1 -MD -DNDEBUG -DWIN32 -D_CONSOLE
-DNO_STRICT -DHAVE_DES_FCRYPT -DPERL_IMPLICIT_CONTEXT
-DPERL_IMPLICIT_SYS -DPERL_MSVCRT_READFIX',
  optimize='-O1 -MD -DNDEBUG',
  cppflags='-DWIN32'
  ccversion='', gccversion='', gccosandvers=''
  intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
  d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10
  ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=4
  alignbytes=8, usemymalloc=n, prototype=define
  Linker and Libraries​:
  ld='link', ldflags ='-nologo -nodefaultlib -release
-libpath​:"E​:\Perl\lib\CORE" -machine​:x86'
  libpth="E​:\DotNet\FrameworkSDK\Lib\" "E​:\Perl\lib\CORE"
  libs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib ol
eaut32.lib netapi32.lib uuid.lib wsock32.lib mpr.lib winmm.lib version.lib
odbc32.lib odbccp32.lib msvcrt.lib
  perllibs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.li
b oleaut32.lib netapi32.lib uuid.lib wsock32.lib mpr.lib winmm.lib
version.lib odbc32.lib odbccp32.lib msvcrt.lib
  libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl56.lib
  Dynamic Linking​:
  dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
  cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -release
-libpath​:"E​:\Perl\lib\CORE" -machine​:x86'

Characteristics of this binary (from libperl)​:
  Compile-time options​: MULTIPLICITY USE_ITHREADS PERL_IMPLICIT_CONTEXT
PERL_IMPLICIT_SYS
  Locally applied patches​:
  ActivePerl Build 633
  Built under MSWin32
  Compiled at Jun 17 2002 21​:33​:05

@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Oct 30, 2002

From @demerphq

So, my vote is that this is a bug. (it works with
("-4\n".."-0\n") but not
("-4\n".."0\n") !!?) Oh, and stripping the "\n" out doesnt change the
results.

V​:\perl\lib\CPAN>perl -e "print qq{-4}..qq{0}"

V​:\perl\lib\CPAN>perl -e "print 0+qq{-4}..0+qq{0}"
-4-3-2-10

I just checked and under the lastest patch of bleadperl there is the same
problem.
Patch to t/op/range/.t to test this behaviour is attached and is below.

Inline Patch
--- source\t\op\range.t	Tue Aug  1 04:32:14 2000
+++ mysource\t\op\range.t	Wed Oct 30 12:51:38 2002
@@ -1,6 +1,6 @@
 #!./perl
 
-print "1..15\n";
+print "1..19\n";
 
 print join(':',1..5) eq '1:2:3:4:5' ? "ok 1\n" : "not ok 1\n";
 
@@ -73,3 +73,9 @@
     print "not ";
 }
 print "ok 15\n";
+
+# These should produce the same results or we don't DWIM
+print join(":","-4".."0")      eq "-4:-3:-2:-1:0" ? "ok 16\n" : "not ok
16\n"; \+print join\("​:"\,"\-4"\.\."\-0"\) eq "\-4​:\-3​:\-2​:\-1​:0" ? "ok 17\\n" : "not ok 17\\n"; \+print join\("​:"\,"\-4\\n"\.\."0\\n"\) eq "\-4​:\-3​:\-2​:\-1​:0" ? "ok 18\\n" : "not ok 18\\n"; \+print join\("​:"\,"\-4\\n"\.\."\-0\\n"\) eq "\-4​:\-3​:\-2​:\-1​:0" ? "ok 19\\n" : "not ok 19\\n";
@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Oct 30, 2002

From @demerphq

range.patch
--- source\t\op\range.t	Tue Aug  1 04:32:14 2000
+++ mysource\t\op\range.t	Wed Oct 30 12:51:38 2002
@@ -1,6 +1,6 @@
 #!./perl
 
-print "1..15\n";
+print "1..19\n";
 
 print join(':',1..5) eq '1:2:3:4:5' ? "ok 1\n" : "not ok 1\n";
 
@@ -73,3 +73,9 @@
     print "not ";
 }
 print "ok 15\n";
+
+# These should produce the same results or we don't DWIM
+print join(":","-4".."0")      eq "-4:-3:-2:-1:0" ? "ok 16\n" : "not ok 16\n";
+print join(":","-4".."-0")     eq "-4:-3:-2:-1:0" ? "ok 17\n" : "not ok 17\n";
+print join(":","-4\n".."0\n")  eq "-4:-3:-2:-1:0" ? "ok 18\n" : "not ok 18\n";
+print join(":","-4\n".."-0\n") eq "-4:-3:-2:-1:0" ? "ok 19\n" : "not ok 19\n";
@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Oct 30, 2002

From @eserte

"Orton, Yves" <yves.orton@​mciworldcom.de> writes​:

Content-type​: text/plain ; charset = "iso-8859-1"

J Kimball (via RT) on Tuesday, October 29, 2002 8​:49 AM
On Mon, Oct 28, 2002 at 08​:18​:24AM -0000, a.shankar@​ti.com
(via RT) wrote​:

I wrote the following program​:

$w1=<STDIN>;
$w2=<STDIN>;
@​numlist = ($w1 .. $w2);
print @​numlist;

[run]
-4 (first input)
0 (second input)

it prints nothing!

I see that you are running Perl on Windows. Try printing a blank line
before you print anything else​:

Im pretty sure that this is not the cause of the problem.

To which Abhishek Shankar said on 29 October 2002 04​:34

The print("\n"); is not solving the problem. In fact a new
problem has comes up. Under Windows if any of the inputs is 0 the program
fails. In Unix the input -4 and 0 prints the required list of numbers. But
the
inputs 0 and 4 fail to print anything.

I agree that this is a bug. It appears to be because the strings '0\n' and
'-4\n' are not being interpreted in numeric context in the .. operator.
(Although I can't duplicate your problem with "0".."4" under unix I suspect
the solution is the same, see below.)

At first I thought this was due to the documented behaviour of the ..
operator. In that "0\n" .. "-4\n" gets evaluated. Since neither have been
used in numeric context they are treated as strings. Since they dont match
the rules for string increment nor for numeric arguments the range operator
returns an empty list. Unfortunatley this explanation doesn't wash​:

E​:\Bin>perl -e "print qq{-4\n}..qq{-0\n}"
-4-3-2-10
E​:\Bin>perl -e "print 0+qq{-4\n}..0+qq{0\n}"
-4-3-2-10

Which is more or less DWIM i think. But this sure isnt​:

E​:\Bin>perl -e "print qq{-4\n}..qq{0\n}"

E​:\Bin>

So, my vote is that this is a bug. (it works with ("-4\n".."-0\n") but not
("-4\n".."0\n") !!?) Oh, and stripping the "\n" out doesnt change the
results.

V​:\perl\lib\CPAN>perl -e "print qq{-4}..qq{0}"

V​:\perl\lib\CPAN>perl -e "print 0+qq{-4}..0+qq{0}"
-4-3-2-10

Luckily the workaround is trivial indeed. Simply coerce numeric context.

@​numlist = (0+$w1 .. 0+$w2);

or

$w1=0+<STDIN>;
$w2=0+<STDIN>;

HTH.

There is a special handling for numeric strings beginning with a "0".
This is to allow things like "01".."31" to preserve the leading zero
for one-digit numbers.

The special handling is triggered if either the left or right operand
starts with "0". For left operands this is OK but I cannot think of a
useful case where only a right operand with leading zero triggers this
handling.

OK​:

$ perl -e 'print join(",", "01" .. "02"), "\n"'
01,02

Strange​:

$ perl -e 'print join(",", "1" .. "02"), "\n"'
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99

With the patch below, Yves' new test cases pass (and all other tests)
and the output from the example below looks better​:

$ ./perl -e 'print join(",", "1" .. "02"), "\n"'
1,2

Regards,
  Slaven

Inline Patch
--- /usr/local/src/bleedperl/pp_ctl.c	Tue Oct 22 20:14:24 2002
+++ ./pp_ctl.c	Wed Oct 30 15:45:46 2002
@@ -946,7 +946,7 @@ PP(pp_flop)
 	if (SvNIOKp(left) || !SvPOKp(left) ||
 	    SvNIOKp(right) || !SvPOKp(right) ||
 	    (looks_like_number(left) && *SvPVX(left) != '0' &&
-	     looks_like_number(right) && *SvPVX(right) != '0'))
+	     looks_like_number(right)))
 	{
 	    if (SvNV(left) < IV_MIN || SvNV(right) > IV_MAX)
 		DIE(aTHX_ "Range iterator outside integer range");


-- 

Slaven Rezic - slaven.rezic@​berlin.de

  tknotes - A knotes clone, written in Perl/Tk.
  http​://ptktools.sourceforge.net/#tknotes

@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Oct 30, 2002

From @demerphq

Slaven Rezic Sent on 30 October 2002 16​:23

There is a special handling for numeric strings beginning with a "0".
This is to allow things like "01".."31" to preserve the leading zero
for one-digit numbers.

Aha!

The special handling is triggered if either the left or right operand
starts with "0". For left operands this is OK but I cannot think of a
useful case where only a right operand with leading zero triggers this
handling.

OK​:

$ perl -e 'print join(",", "01" .. "02"), "\n"'
01,02

Strange​:

$ perl -e 'print join(",", "1" .. "02"), "\n"'
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,4
5,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,
66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86
,87,88,89,90,91,92,93,94,95,96,97,98,99

With the patch below, Yves' new test cases pass (and all other tests)
and the output from the example below looks better​:

$ ./perl -e 'print join(",", "1" .. "02"), "\n"'
1,2

Nice.

:-)

Yves

@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Dec 7, 2002

From @jhi

I think this issue got resolved, I'm marking the problem
ticket as resolved, too.

@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Dec 7, 2002

@jhi - Status changed from 'new' to 'resolved'

@p5pRT p5pRT closed this Dec 7, 2002
@p5pRT p5pRT added the Severity Low label Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.