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

Conclusion: Don't use $[ as anything other than 0 (as presented by a couple of other monks so far.

--MidLifeXis

The tomes, scrolls etc are dusty because they reside in a dusty old house, not because they're unused. --hangon in this post


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.