mwp has asked for the wisdom of the Perl Monks concerning the following question:
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:
print $#ary1, " ", $#ary2, "\n";#!/usr/bin/perl -w use strict; my @ary1 = (0..5); my @ary2 = (1,3,5); print scalar(@ary1), " ", scalar(@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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Arrays: Last-index special variable vs. scalar values
by btrott (Parson) on Nov 16, 2000 at 00:20 UTC | |
|
(Guildenstern) RE: Arrays: Last-index special variable vs. scalar values
by Guildenstern (Deacon) on Nov 16, 2000 at 00:23 UTC | |
|
(doh) RE: Arrays: Last-index special variable vs. scalar values
by mwp (Hermit) on Nov 16, 2000 at 00:30 UTC | |
|
Re: Arrays: Last-index special variable vs. scalar values
by snax (Hermit) on Nov 16, 2000 at 13:35 UTC | |
by tenatious (Beadle) on Nov 16, 2000 at 22:00 UTC | |
by Dominus (Parson) on Nov 16, 2000 at 22:07 UTC | |
by Dominus (Parson) on Nov 16, 2000 at 22:17 UTC | |
|
Re: Arrays: Last-index special variable vs. scalar values
by zodiac (Beadle) on Nov 16, 2000 at 17:52 UTC |