There is more than one way to do it. But not every way is a good one. For example, you mention scalar(@array) - 1 to get the last index. This is not a good way to get the last index. It is a very bad way to get the last index.

Just like in real language, it isn't only about what you say, but also HOW you express that. scalar(@array) does not mean "the last index plus one" or even "the number that will be the index if you add a new element". It says "the number of elements".

It would be sort-of okay to use scalar(@array) - 1 if $#array did not exist. But $#array does exist, and it says "the index of the last element in @array".

$#arrayThe index of the last element
$#array + 1The index of the last element plus one
@arrayThe number of elements
@array - 1The number of elements minus one

For the same reason, $#array + 1 would be WRONG if you want to get the number of elements.

Sometimes, unless is better than if. Sometimes unless is wrong and if is right. Sometimes you need to reverse the expressions (open or die versus die unless open) to get the perfect sentence.

TIMTOWDTI. NYPAGO. (Now You Pick A Good One)

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

P.S. $[ is irrelevant. Yes, it would sort of support my point. But even without $[, using $#array+1 instead of @array, or @array-1 instead of $#array would be wrong when you use code that doesn't express what you meant.


In reply to Re: TIMTOWTDI doesn't mean invent an outlandish approach (usually) by Juerd
in thread TIMTOWTDI doesn't mean invent an outlandish approach (usually) by davido

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.