in reply to Getting range from N..end with list slice

Maybe your friend is thinking of negative indexes.

$a[-1] # last element $a[-2] # second-last element

This is much older than 5.10. It also works on slices

@a[-2, -1] # second-last and last element

But you have to keep in mind that ".." has nothing to do with slices. If the LHS is greater than the RHS, it returns an empty list.

>perl -E"@a = qw( a b c d e f ); say for @a[4..-1]" >perl -E"@a = qw( a b c d e f ); say for @a[-3..-1]" d e f >perl -E"@a = qw( a b c d e f ); say for @a[-1..-3]" >perl -E"@a = qw( a b c d e f ); say for @a[reverse -3..-1]" f e d >perl -E"@a = qw( a b c d e f ); say for reverse @a[-3..-1]" f e d

Replies are listed 'Best First'.
Re^2: Getting range from N..end with list slice
by Marshall (Canon) on Nov 28, 2010 at 17:47 UTC
    I also played around with negative indicies, but the problem is that say with [-3..-1], in general we don't know what this -3 value is supposed to be because it is counted backwards from the right. All we know is that we don't want [0,1,2](counted from the left).

    This >perl -E"@a = qw( a b c d e f ); say for @a[4..-1]" would work except for that pesky LHS > RHS returns empty list rule! My friend may be thinking that there is some new syntax like: @a[4..eol], where "eol" is some stand-in for the largest positive index whatever that happens to be. Or alternately some way to determine, not the value at [-1], but the positive index that it would correspond to.

    I also wondered if there was some sort of new syntax that would express the idea of "give me everything except the stuff at these particular indicies...", but was unable to find anything like that.

      ... that pesky LHS > RHS returns empty list rule!

      Not exactly pretty, and still won't work with a list, but:

      >perl -wMstrict -le "my @ra = qw(a b c d e f g h); my $i = 4; print qq{'$_'} for @ra[$i - @ra .. -1]; " 'e' 'f' 'g' 'h'

        It's actually even simpler:

        @ra[$i .. $#ra]

        The difference between an array slice and a list slice is that you have the array before the slice is evaluated.