Do you know where your variables are? | |
PerlMonks |
Arrays: Last-index special variable vs. scalar valuesby mwp (Hermit) |
on Nov 16, 2000 at 00:17 UTC ( [id://41862]=perlquestion: print w/replies, xml ) | Need Help?? |
mwp has asked for the wisdom of the Perl Monks concerning the following question:
$#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:
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"; Which returns the following:
"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:
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
Back to
Seekers of Perl Wisdom
|
|