http://www.perlmonks.org?node_id=44637

clash has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

One way I know of is:
$array[-1]
This counts 'backward' from the end of the array.

Any others?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I get the last element of an array?
by extremely (Priest) on Dec 03, 2000 at 11:49 UTC
    $array[$#array]
Re: How do I get the last element of an array?
by eg (Friar) on Dec 03, 2000 at 15:29 UTC
    Here's a rather unconventional way:
    push @array, my $val = pop @array;
Re: How do I get the last element of an array?
by InfiniteSilence (Curate) on Dec 11, 2000 at 21:54 UTC
      You could also use:
      perl -e "@m-(1,2,3);print $m[-1]"
      python -c 'm=[1,2,3]; print m[-1]'
Re: How do I get the last element of an array?
by Anonymous Monk on Dec 04, 2000 at 02:22 UTC
    You can use $#array as the index of the last element in @array.

    Originally posted as a Categorized Answer.