http://qs1969.pair.com?node_id=204925


in reply to How do I find the index of the last element in an array?

Just for any newbies...

Don't forget array indicies start at zero


@letterList = ("a", "b", "c", "d");

a is at position 0
b "   "     "    1
c "   "     "    2
...

Hope that helps out anyone who fell for the oldest trick in the book

Mark
  • Comment on Re: How do I find the index of the last element in an array?

Replies are listed 'Best First'.
Re: Answer: How do I find the index of the last element in an array?
by jens (Pilgrim) on Oct 14, 2002 at 04:30 UTC
    Hmm....actually, that doesn't really help at all!

    If you want to know the index of the last element of an array, do this:

    @letterList = ("a", "b", "c", "d"); #refer to the last array index #using '$#array' syntax: print $#letterList;
    This will, of course, output "3".

    You can also refer to the value of the last element of the array
    using negative indices, e.g. $letterList[-1] will also refer to "d".

    --
    Microsoft delendum est.