I can't see why you need to avoid using array indices at all.

First of all, you can get the start of the array index (of all arrays, actually) by using the special variable $[. The default value for this variable is 0. You are discouraged to modify this value in your script. Any change to this variable in other files (or packages? Not really sure about that one. See perlvar for more info. It's "files".) such as modules, will be visible in that file only. In short: I find it perfectly reasonable to always assume that the start index is 0 for your script. Always. Unless you, yourself, change it to something else.

Second, the last index for an array @array is $#array. No, $#array is not always 1 less than scalar(@array), that depends on the value for $[; but it is always the last index of your array.

In summary: it's perfectly 100% safe to write:

for my $i ($[ .. $#array) { $foo[$i] = $array[$i]; }
but at least I wouldn't mind at all if you hardcoded a 0 for that $[.

In reply to Re: Array indices by bart
in thread Array indices by Limbic~Region

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.