$#array vs. @array

Simple question, simple answer, right? Well, maybe not. I was rambling along in the chatterbox this afternoon and said something that caused me to pause. After chewing my bottom lip for a moment I put together a test script and verified that I was wrong. But then I checked the perldocs and verified I was right. Now I'm just confused!

For a long time I have "known" that $#array returns the index of the last element in @array. From perlman:perldata:

  The length of an array is a scalar value. You may find the length of array @days by evaluating $#days, as in csh. (Actually, it's not the length of the array, it's the subscript of the last element, because there is (ordinarily) a 0th element.)

So far, so good. Essentially what I yammered into the chatterbox was something along the lines of: Beware the distinction between scalar @array and $#array. The former will return the number of elements in the list, while the latter will tell you the index of the last element. For example, if you have two lists (0..5) and (1,3,5) the special variable will return 5 in both instances, while the scalar values are 6 and 3, respectively. This is what caused me to double check myself by putting together a quick program:

#!/usr/bin/perl -w use strict; my @ary1 = (0..5); my @ary2 = (1,3,5); print scalar(@ary1), " ", scalar(@ary2), "\n";
print $#ary1, " ", $#ary2, "\n";

Which returns the following:

alakaboo:~$ ./test.pl 6 3 5 2

"Good, just as I... WAIT A MINUTE!" Imagine my surprise. That two should be a five! From perlman:perldata again, less than a page from the first pasting:

  If you evaluate a named array in a scalar context, it returns the length of the array. (Note that this is not true of lists, which return the last value, like the C comma operator, nor of built-in functions, which return whatever they feel like returning.) The following is always true: scalar(@whatever) == $#whatever - $[ + 1; Version 5 of Perl changed the semantics of $[: files that don't set the value of $[ no longer need to worry about whether another file changed its value. (In other words, use of $[ is deprecated.) So in general you can assume that scalar(@whatever) == $#whatever + 1;

So tell me, is this a Perl 5.6 bug that's known about but not discussed in polite company, are the Perl docs contrary and confusing, or has someone been slipping something into my Pepsi?

Alakaboo


In reply to Arrays: Last-index special variable vs. scalar values by mwp

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.