Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How do I find the index of the last element in an array?

by heezy (Monk)
on Oct 13, 2002 at 17:38 UTC ( [id://204925]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://204925]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-18 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found