Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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 exploiting the Monastery: (8)
As of 2014-09-17 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (57 votes), past polls