There's yet another pitfall associated with the use of  $#array to determine the number of elements in an array. It's possible to assign to  $[ (see perlvar) to change the base index of Perl arrays. Doing so will result in your source entry appendage(s) receiving a sharp whack, so Don't Do That!SM

Of course, it's possible to use  $#array for this purpose in a base-agnostic way:

c:\@Work\Perl\monks>perl -wMstrict -le "print qq{perl array base is $[}; ;; my @ra = (); print qq{A: array (@ra) is empty} if $#ra + 1 - $[ == 0; ;; @ra = (1); print qq{B: array (@ra) is empty} if $#ra + 1 - $[ == 0; " perl array base is 0 A: array () is empty c:\@Work\Perl\monks>perl -wMstrict -le "$[ = 1; print qq{perl array base is $[}; ;; my @ra = (); print qq{A: array (@ra) is empty} if $#ra + 1 - $[ == 0; ;; @ra = (1); print qq{B: array (@ra) is empty} if $#ra + 1 - $[ == 0; " Use of assignment to $[ is deprecated at -e line 1. perl array base is 1 A: array () is empty

But an array  @array evaluated in scalar context always yields the number of array elements, so I don't understand why one would ever do this in any other way.

BTW: It's possible to assign a number other than 0 or 1 to $[, but then you're really on thin ice, so Double-Plus-Don't Do That!


Give a man a fish:  <%-{-{-{-<


In reply to Re^3: Perl Array - What an array contains by AnomalousMonk
in thread Perl Array - What an array contains by rahulme81

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.