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

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.

Replies are listed 'Best First'.
Re^3: Getting range from N..end with list slice
by AnomalousMonk (Archbishop) on Nov 28, 2010 at 18:42 UTC
    ... 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.

        ... even simpler:  @ra[$i .. $#ra]

        Yes, but that's also the prettier version! My intent, clumsily realized, was to endorse the idea that negative indexing off the end of an array (or, in the OP, a list) isn't the way to go.