in reply to Reasoning behind -1 not working in slices?

Splutty,
To add to the previous answer(s), -1 is not a macro to say "the last element". It is an offset. Perl allows you to use negative offsets to access an array from either end. The range operator (see perlop) only works in ascending order though Perl 6 will greatly increase your flexibility.

Assume for a second you have a 4 element array and you want to take the slice 2 .. -2.

2 with positive offset my @array = (1, 2, 3, 4); -2 with negative offset
Don't you think it violates the principal of least suprise to expect Perl to realize you are taking an array slice, calculate the positive index of the negative offset and then create an ascending range for you?

Update: Phrased the last question better.

Cheers - L~R

Replies are listed 'Best First'.
Re^2: Reasoning behind -1 not working in slices?
by Splutty (Initiate) on Aug 15, 2006 at 13:02 UTC

    I understand the principle of why it doesn't work. However I maintain that it would not be unlogical to have as a 'last element' identifier a negative number going from the end of the array.

    But since perl doesn't actually use the list as array indexing, but to actually build a list, which is then subsequently used to get a slice from the array, this probably won't be possible/easy.

    Thanks for all the replies :) It has greatly improved my understanding of slice definitions.