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

Re^2: What’s the best way to get the last N elements of a Perl array?

by linuxer (Deacon)
on Apr 20, 2009 at 11:26 UTC ( #758674=note: print w/ replies, xml ) Need Help??


in reply to Re: What’s the best way to get the last N elements of a Perl array?
in thread What’s the best way to get the last N elements of a Perl array?

Beware: What does that solution do, if $n > @source?

See:
$ perl -le '@a = 1..3; @b = @a[(@a-5)..$#a]; print "<$_>" for @b;' <2> <3> <1> <2> <3>


Comment on Re^2: What’s the best way to get the last N elements of a Perl array?
Select or Download Code
Re^3: What’s the best way to get the last N elements of a Perl array?
by Utilitarian (Vicar) on Apr 20, 2009 at 13:09 UTC
    @last_n=@source[(@source-$n)..$#source]if ($n<=@scource);
    Would be the solution there I guess

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2015-07-06 23:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (85 votes), past polls