vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: What’s the best way to get the last N elements of a Perl array?
by moritz (Cardinal) on Apr 20, 2009 at 11:38 UTC
    I've not tested it, but I usually use something like this:
    use List::Util qw(max); @last_n = @source[max(0, @source - $n) .. $#source];

    Update: Fixed offby1-error pointed out by AnomalousMonk++

Re: What’s the best way to get the last N elements of a Perl array?
by Jenda (Abbot) on Apr 20, 2009 at 12:27 UTC
Re: What’s the best way to get the last N elements of a Perl array?
by Corion (Patriarch) on Apr 20, 2009 at 13:19 UTC
Re: What’s the best way to get the last N elements of a Perl array?
by svenXY (Deacon) on Apr 20, 2009 at 11:29 UTC
    Hi,

    add
    @last_n = grep { defined($_) } @last_n;
    and it will work - given that there were no undef elements in the array that you want to keep
    Regards,
    svenXY
Re: What’s the best way to get the last N elements of a Perl array?
by svenXY (Deacon) on Apr 20, 2009 at 10:34 UTC
    Note:If the array has less than N, I don't want a bunch of undefs in the return value.

    Seems like you already tried something or at least thought about your task. Was there any outcome (a.k.a. code) that you can show us?

    Regards,
    svenXY
Re: What’s the best way to get the last N elements of a Perl array?
by linuxer (Curate) on Apr 20, 2009 at 11:29 UTC

    If undef is no valid entry in your array, you could use your solution and grep the defined values from your slice result.

    @last_n = grep { defined $_ } @source[ -$n .. -1 ];

    Update:

    As svenXY mentioned: my solution does not work! :o(

    $ perl -wle '@a = 1..3; @b = grep { defined $_ } @a[-4 .. -1]; print " +<$_>" for @b;' Modification of non-creatable array value attempted, subscript -4 at - +e line 1. $ perl -wle '@a = 1..3; @b = @a[-4 .. -1]; @b = grep {defined $_} @b; +print "<$_>" for @b;' <1> <2> <3>

    Can someone please enlighten me, what's the problem with my attempt?

    2nd Update:

    During my research I found an explanation in Modification of non-creatable array value attempted (it's a slightly other topic, but at least the same error message) and grep's perldoc.

    I came to the conclusion, that the error is thrown, because grep tries to alias the list values; and this fails for not existent array elements.

      nope, does not work for me: Modification of non-creatable array value attempted, subscript -9 at array_ende.pl line 23. whereas it does work as a two-liner (see above)
      Regards,
      svenXY
Re: What’s the best way to get the last N elements of a Perl array?
by Utilitarian (Vicar) on Apr 20, 2009 at 11:21 UTC
    @last_n=@source[(@source-$n)..$#source];

      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
Re: What’s the best way to get the last N elements of a Perl array?
by Anonymous Monk on Apr 20, 2009 at 10:26 UTC
    Can you define best?