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]); }