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

shift fails for 0 #343

Closed
p5pRT opened this issue Aug 5, 1999 · 12 comments
Closed

shift fails for 0 #343

p5pRT opened this issue Aug 5, 1999 · 12 comments

Comments

@p5pRT
Copy link

p5pRT commented Aug 5, 1999

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

Searchable as RT1174$

@p5pRT
Copy link
Author

p5pRT commented Aug 5, 1999

From mbc502@sbe1351.swissptt.ch


The "shift" function fails when the item shifted is 0. This is illustrated
by the following program​:

  #!/usr/bin/perl -w
  #shift.pl 19990805 NHA
  #Illustrate bug with shift

  @​list = (-2,-1, 0, 1);
  while ($elt = shift @​list) { print "list element = $elt\n"; }
  print "End-Of-List detected\n";

The output on my machine is​:

  list element = -2
  list element = -1
  End-Of-List detected



Site configuration information for perl 5.00503​:

Configured by root at Wed Feb 9 11​:38​:32 MET 2000.

Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration​:
  Platform​:
  osname=hpux, osvers=11.00, archname=PA-RISC2.0
  uname='hp-ux sbe1351 b.11.00 e 9000889 274894302 8-user license '
  hint=previous, useposix=true, d_sigaction=define
  usethreads=undef useperlio=undef d_sfio=undef
  Compiler​:
  cc='cc', optimize='-O', gccversion=
  cppflags='-D_HPUX_SOURCE -Aa +DA2.0W'
  ccflags ='-D_HPUX_SOURCE -Aa +DA2.0W'
  stdchar='unsigned char', d_stdstdio=define, usevfork=false
  intsize=4, longsize=8, ptrsize=8, doublesize=8
  d_longlong=undef, longlongsize=, d_longdbl=define, longdblsize=16
  alignbytes=8, usemymalloc=y, prototype=define
  Linker and Libraries​:
  ld='ld', ldflags ='+DA2.0W'
  libpth=/usr/lib/pa20_64
  libs=-lnsl -lgdbm -ldl -ldld -lm -lc
  libc=/usr/lib/pa20_64/libc.sl, so=sl, useshrplib=false, libperl=libperl.a
  Dynamic Linking​:
  dlsrc=dl_hpux.xs, dlext=sl, d_dlsymun=undef, ccdlflags='-Wl,-E -Wl,-B,deferred '
  cccdlflags='+z', lddlflags='-b'

Locally applied patches​:
 


@​INC for perl 5.00503​:
  /usr/local/perl5/lib/5.00503/PA-RISC2.0
  /usr/local/perl5/lib/5.00503
  /usr/local/perl5/lib/site_perl/5.005/PA-RISC2.0
  /usr/local/perl5/lib/site_perl/5.005
  .


Environment for perl 5.00503​:
  HOME=/mbc/mbc502/mbc502
  LANG (unset)
  LANGUAGE (unset)
  LC_CTYPE=C
  LD_LIBRARY_PATH (unset)
  LOGDIR (unset)
  PATH=/mbc/mbc502/mbc502/tool​:/mbc/mbc502/mbc502/bin​:/usr/local/perl5/bin​:/apps/oracle/V800400/bin​:/usr/bin​:/opt/ansic/bin​:/usr/ccs/bin​:/usr/contrib/bin​:/opt/nettladm/bin​:/opt/fc/bin​:/opt/fcms/bin​:/opt/upgrade/bin​:/opt/pd/bin​:/usr/bin/X11​:/usr/contrib/bin/X11​:/opt/resmon/bin​:/opt/java/bin​:/opt/hparray/bin​:/opt/pred/bin​:/opt/aCC/bin​:/opt/langtools/bin​:/opt/imake/bin​:/opt/hpnp//bin​:.​:/mbc/mbc502/mbc502/shell​:/usr/local/emacs/bin/​:/apps/oracle/MBC502_HOME/bin
  PERL_BADLANG (unset)
  SHELL=zsh

@p5pRT
Copy link
Author

p5pRT commented Aug 5, 1999

From [Unknown Contact. See original ticket]

MBC502 wrote​:

    @​list = \(\-2\,\-1\, 0\, 1\);
    while \($elt = shift @​list\)  \{ print "list element = $elt\\n"; \}
    print "End\-Of\-List detected\\n";

The output on my machine is​:

    list element = \-2
    list element = \-1
    End\-Of\-List detected

while (0) won't execute anything.
try​:

  while (defined($elt = shift @​list)) { ... }

or something similar.
Rainer

--
Rainer J. H. Brandt tel 0172/9593205
Brandt & Brandt Computer GmbH fax 02225/946666
Am Wiesenpfad 6
D-53340 Meckenheim rainer.brandt@​commerzbank.com

@p5pRT
Copy link
Author

p5pRT commented Aug 5, 1999

From [Unknown Contact. See original ticket]

Right you are. Thanks for setting me straight.

NHA


Norman Azadian; naz@​ascom.ch; tel​: +41 31 999-3223 (fax​: -1019)
Ascom Schweiz AG; Freiburgstrasse 251; CH-3018 Bern; Switzerland

On Thu, 5 Aug 1999, Rainer J. H. Brandt wrote​:

MBC502 wrote​:

    @​list = \(\-2\,\-1\, 0\, 1\);
    while \($elt = shift @​list\)  \{ print "list element = $elt\\n"; \}
    print "End\-Of\-List detected\\n";

The output on my machine is​:

    list element = \-2
    list element = \-1
    End\-Of\-List detected

while (0) won't execute anything.
try​:

while (defined($elt = shift @​list)) { ... }

or something similar.
Rainer

--
Rainer J. H. Brandt tel 0172/9593205
Brandt & Brandt Computer GmbH fax 02225/946666
Am Wiesenpfad 6
D-53340 Meckenheim rainer.brandt@​commerzbank.com

@p5pRT
Copy link
Author

p5pRT commented Aug 5, 1999

From [Unknown Contact. See original ticket]

shift isn't failing. You're testing whether the result
is true. 0 isn't.

Probably you should use foreach() instead of while shift.

--tom

@p5pRT
Copy link
Author

p5pRT commented Aug 5, 1999

From [Unknown Contact. See original ticket]

Surely the only way to tell when the list is empty is to look at $#list or
@​list in a scalar context. Undef is a legal value to store in a list​:

  @​list = (-2, -1, undef, 0, 1);
  while (@​list) {
  $elt = shift @​list;
  ...
  }
  print "List exhausted\n";

You can't distinguish between the undef returned becaus the list is empty
and the undef returned because there was an undef as the list's first
value, so you must look at the list's length.

Mike

On Thu, 5 Aug 1999, Rainer J. H. Brandt wrote​:

MBC502 wrote​:

    @​list = \(\-2\,\-1\, 0\, 1\);
    while \($elt = shift @​list\)  \{ print "list element = $elt\\n"; \}
    print "End\-Of\-List detected\\n";

The output on my machine is​:

    list element = \-2
    list element = \-1
    End\-Of\-List detected

while (0) won't execute anything.
try​:

while (defined($elt = shift @​list)) { ... }

or something similar.
Rainer

--
Rainer J. H. Brandt tel 0172/9593205
Brandt & Brandt Computer GmbH fax 02225/946666
Am Wiesenpfad 6
D-53340 Meckenheim rainer.brandt@​commerzbank.com

--
mike@​stok.co.uk | The "`Stok' disclaimers" apply.
http​://www.stok.co.uk/~mike/ | PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
  | 65 F3 3F 1D 27 22 B7 41
stok@​colltech.com | Collective Technologies (work)

@p5pRT
Copy link
Author

p5pRT commented Aug 5, 1999

From [Unknown Contact. See original ticket]

You can't distinguish between the undef returned becaus the list is empty
and the undef returned because there was an undef as the list's first
value, so you must look at the list's length.

I keep expecting someone is going to propose that

  exists $a[$i]

means

  $#a >= $i

--tom, who's kidding.

@p5pRT
Copy link
Author

p5pRT commented Aug 5, 1999

From [Unknown Contact. See original ticket]

On Thu, 5 Aug 1999, Tom Christiansen wrote​:

I keep expecting someone is going to propose that

exists $a\[$i\]

means

$\#a >= $i

--tom, who's kidding.

Can you expect retroactively? :-)

  Date​: Fri, 31 Oct 1997 11​:55​:09 -0700 (MST)
  From​: Joseph N. Hall <joseph@​cscaper.com>
  To​: perl5-porters@​perl.org
  Subject​: exists on array elements?

  It seems like it would be handy if exists worked on arrays as well as
  hashes. It would make out of range testing much easier. I would have
  negative subscripts return false.

  Thoughts?

  -joseph

--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case​: http​://www.rahul.net/jeffrey/ovs/

@p5pRT
Copy link
Author

p5pRT commented Aug 5, 1999

From [Unknown Contact. See original ticket]

On Thu, Aug 05, 1999 at 11​:09​:27AM -0700, "Tom Phoenix" wrote​:

On Thu, 5 Aug 1999, Tom Christiansen wrote​:

I keep expecting someone is going to propose that
exists $a[$i]
means
$#a >= $i
--tom, who's kidding.
Can you expect retroactively? :-)
Date​: Fri, 31 Oct 1997 11​:55​:09 -0700 (MST)
From​: Joseph N. Hall <joseph@​cscaper.com>
To​: perl5-porters@​perl.org
Subject​: exists on array elements?
It seems like it would be handy if exists worked on arrays as well as
hashes. It would make out of range testing much easier. I would have
negative subscripts return false.
Thoughts?
-joseph

How about?

  # Either replace element $i, or append to array...
  first_exists $array[$i], $array[@​array] = 4;

to go along with​:

  {
  # Temporarily override tmppath...
  local(first_defined $this->{tmppath}, $default_tmppath) = "/tmp";
  ...
  $this->write_temp_file;
  }

mark (also <half> kidding)

--
markm@​nortelnetworks.com/mark@​mielke.cc/markm@​ncf.ca __________________________
. . _ ._ . . .__ . . ._. .__ . . . .__ | CUE Development (4Y21)
|\/| |_| |_| |/ |_ |\/| | |_ | |/ |_ | Nortel Networks
| | | | | \ | \ |__ . | | .|. |__ |__ | \ |__ | Ottawa, Ontario, Canada

  One ring to rule them all, one ring to find them, one ring to bring them all
  and in the darkness bind them...

  http​://mark.mielke.cc/

@p5pRT
Copy link
Author

p5pRT commented Aug 5, 1999

From [Unknown Contact. See original ticket]

Tom Christiansen writes​:

You can't distinguish between the undef returned becaus the list is empty
and the undef returned because there was an undef as the list's first
value, so you must look at the list's length.

I keep expecting someone is going to propose that

exists $a\[$i\]

means

$\#a >= $i

I think this might be useful in some situations. Note that exists
$a[$i] can do more. IIRC extending the array (either by $#a = 1000,
or by $a[1000] = 15) leaves some special SV* inside the array of SV*
(Nullsv?). C<exists> could check for these special values too.

Ilya

@p5pRT
Copy link
Author

p5pRT commented Aug 5, 1999

From [Unknown Contact. See original ticket]

On Thu, Aug 05, 1999 at 01​:55​:25PM -0600, Tom Christiansen wrote​:

I think this might be useful in some situations. Note that exists
$a[$i] can do more. IIRC extending the array (either by $#a = 1000,
or by $a[1000] = 15) leaves some special SV* inside the array of SV*
(Nullsv?). C<exists> could check for these special values too.

To what purpose?

For the same purpose exists $foo{bar} exists ;-), and for whatever
purpose exists $a[$b] might be implemented.

Ilya

@p5pRT
Copy link
Author

p5pRT commented Aug 5, 1999

From [Unknown Contact. See original ticket]

I think this might be useful in some situations. Note that exists
$a[$i] can do more. IIRC extending the array (either by $#a = 1000,
or by $a[1000] = 15) leaves some special SV* inside the array of SV*
(Nullsv?). C<exists> could check for these special values too.

To what purpose?

--tom

@p5pRT
Copy link
Author

p5pRT commented Aug 31, 1999

From [Unknown Contact. See original ticket]

This is a bug report for perl from naz@​ascom.com,
generated with the help of perlbug 1.26 running under perl 5.00503.

-----------------------------------------------------------------
The "shift" function fails when the item shifted is 0. This is illustrated
by the following program​:

#!/usr/bin/perl -w
#shift.pl 19990805 NHA
#Illustrate bug with shift

@​list = (-2,-1, 0, 1);
while ($elt = shift @​list) { print "list element = $elt\n"; }
print "End-Of-List detected\n";

The output on my machine is​:

list element = -2
list element = -1
End-Of-List detected
-----------------------------------------------------------------

See change below. I think you wanted to test the length of
the array, not the first element and shift.

[user1@​hoho0 test]$ cat test.pl
#!/usr/local/bin/perl -w
#shift.pl 19990805 NHA
#Illustrate bug with shift

@​list = (-2,-1, 0, 1);
# while ($elt = shift @​list) { print "list element = $elt\n"; }
while (scalar(@​list)) {
  $elt = shift @​list;
  print "list element = $elt\n";
}
print "End-Of-List detected\n";
[user1@​hoho0 test]$ ./test.pl
list element = -2
list element = -1
list element = 0
list element = 1
End-Of-List detected
[user1@​hoho0 test]$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant