I would have used '-1' as the subscript. Would have. As of about 5 minutes ago I changed my mind as to the appropriateness of using -1 as a subscript.
Consider the following blocks...
$[ = -4; @a = (qw(a b c d e f g h i j)); print $a[-1], "\n"; print $a[$#a], "\n"; __DATA__ d j
and
$[ = 0; @a = (qw(a b c d e f g h i j)); print $a[-1], "\n"; print $a[$#a], "\n"; __DATA__ j j
There is some DWIMmery with the -1 option that could cause problems, especially if you are working on old, or someone else's code of unknown usage or localization of the $[ variable. And yes, I have read the disclaimers not to use the $[ variable in the fine manual. I didn't say that I would use the variable. :-)
Update: This is perl, v5.8.8 built for PA-RISC2.0
Comparison
Update #2: Comparison of perl binary available on this machine:
for x in 100 1 0 -1 -6 -7 -11 -12 -99; do perl -le "\$[=$x; my @ra=qw(a b c d e f); print \$[, ': -1:' , \$r +a[-1], ': $#: ', \$ra[\$#ra]" done
This is perl, version 5.005_02 built for PA-RISC1.1
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 -12: -1: f: $#: -99: -1: f: $#:
This is perl, v5.8.8 built for PA-RISC2.0
100: -1: f: $#: f 1: -1: f: $#: f 0: -1: f: $#: f -1: -1: a: $#: f -6: -1: f: $#: f -7: -1: : $#: e -11: -1: : $#: a -12: -1: : $#: -99: -1: : $#:
Conclusion: Don't use $[ as anything other than 0 (as presented by a couple of other monks so far.
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.
by MidLifeXis
in thread A Better Way to Find the Position of the Last Non-Whitespace Character in the Last Element of an Array.
by NateTut
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |