in reply to Re^6: Sub-setting an Array
in thread Sub-setting an Array

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.

Replies are listed 'Best First'.
Re^8: Sub-setting an Array
by kgherman (Novice) on Sep 05, 2015 at 01:33 UTC
    Makes perfect sense! Thanks for your feedback.
Re^8: Sub-setting an Array (sliding window is fifo/queue...?)
by Anonymous Monk on Sep 05, 2015 at 02:27 UTC

    Interesting, I have never heard of "sliding windows" before.... Well, how would you say that in English?

    Great question

    Maybe a FIFO/Queue of size N?

    Hmm, there is a Sliding window protocol, haha