Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I get the last element of an array?

by clash (Novice)
on Dec 03, 2000 at 10:45 UTC ( [id://44637]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-16 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found