Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

( #1868=categorized question: print w/ replies, xml ) Need Help??
Contributed by vroom on Jan 08, 2000 at 08:18 UTC
Q&A  > arrays


Answer: How do I find the index of the last element in an array?
contributed by vroom

You can find it in the special variable $#arrayname

For example:

@foo=('b','a','r'); $lastindex=$#foo; #in this case 2 the index of 'r'
Answer: How do I find the index of the last element in an array?
contributed by chromatic

You can also use negative array indexes:

my @demo = ("one", "two", "three", "four"); print $demo[-1];
The result is "four".
Answer: How do I find the index of the last element in an array?
contributed by ChrisS

Be careful... you can change the starting index value to something other than zero. I would not recommend doing so, but if you did, you'd get the wrong value by using scalar.

Let me show you what I mean:

# change the starting index -- usually not a good idea. $[ = 2; # create a sample array @data = qw(one two three four); # try to find the last index value $bad = @data - 1; $still_bad = scalar @data - 1; $good = $#data; # what did we find? print "bad: $bad\n"; # gives 3 print "still bad: $still_bad\n"; # gives 3 print "good: $good\n" # gives 5

So I would recommend two things:

  1. Don't change the starting index value without a very good reason.
  2. Use the $#array_name syntax if you really need the last index value. (Usually, you want the last element, and can just use the negative subscript technique mentioned above.)

Trying to debug someone else's code when they've changed $[ can be particularly annoying.

Answer: How do I find the index of the last element in an array?
contributed by heezy

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
Answer: How do I find the index of the last element in an array?
contributed by Anonymous Monk

#!/usr/bin/perl @A=(1,2,3); print scalar @A-1; # Index is "scalar @A - 1" !!
Edited: davido added code tags.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (12)
    As of 2014-07-29 18:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (225 votes), past polls