Well, how would you say that in English?
Something like "Stop if the difference between the position of the element we're on and the length of the array is smaller than the length of the window we want", right? So in Perl that might be as simple as:
last if $elements - $offset < 3;
Which will stop the whole loop as soon as it's true. Add it after the next statement and see what happens.
Edit: If you are really wanting to get a sliding subset of the array, you wouldn't want to use splice since as you can see it changes the original array and soon you have no elements left! But making a copy of the array each time through a loop, as I did in the example, is horribly inefficient, and the example is only designed to explain the use of splice, which is what the OP asked for ...
The way forward always starts with a minimal test.
|