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

@last_n=@source[(@source-$n)..$#source];
  • Comment on Re: What’s the best way to get the last N elements of a Perl array?
  • Download Code

Replies are listed 'Best First'.
Re^2: What’s the best way to get the last N elements of a Perl array?
by linuxer (Curate) on Apr 20, 2009 at 11:26 UTC

    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>
      @last_n=@source[(@source-$n)..$#source]if ($n<=@scource);
      Would be the solution there I guess