in reply to Traversing succeding sets of an array
This works for $n between 1 and the number of elements, inclusive.
It takes advantage of array slices when calling the subroutine.
sub do_something_with { print "@_\n"; } my @elements = qw( a b c d e f g ); my $n = 2; for my $i ($n-1 .. $#elements) { do_something_with(@elements[($i-($n-1))..$i]); }
$n = 2 produces:
a b c b c d c d e d e f e f g
$n = 4 produces:
a b c d b c d e c d e f d e f g
|
|---|