in reply to Re^2: A Better Way to Find the Position of the Last Non-Whitespace Character in the Last Element of an Array.
in thread A Better Way to Find the Position of the Last Non-Whitespace Character in the Last Element of an Array.

Interesting. Running the same test on my ActiveState Win32 Perl 5.8.2 build 808, I get results indicating that -1 more reliably accesses the last element of an array.

The following one-liner was run repeatedly with  $[ initialized to different values:

>perl -wMstrict -le "$[ = 100; my @ra = qw(a b c d e f); print '$[: ', $[, ' -1: ', $ra[-1], ' $#: ', $ra[$#ra];" $[: 100 -1: f $#: f
Output of successive runs of the one-liner:
$[: 100 -1: f $#: f $[: 1 -1: f $#: f $[: 0 -1: f $#: f $[: -1 -1: f $#: f $[: -6 -1: f $#: f $[: -7 -1: f $#: e $[: -11 -1: f $#: a Use of uninitialized value in print at -e line 1. $[: -12 -1: f $#: Use of uninitialized value in print at -e line 1. $[: -99 -1: f $#:
Perhaps more support for the don't do that part of the discussion in the docs about assigning to  $[ a value other than 0?

(BTW, the latest perlvar sez wrt  $[ that As of release 5 of Perl, assignment to $[ is treated as a compiler directive, and cannot influence the behavior of any other file, so old code that plays too fast and loose with  $[ may be broken anyway.)

  • Comment on Re^3: A Better Way to Find the Position of the Last Non-Whitespace Character in the Last Element of an Array.
  • Select or Download Code