Another approach, also using List::Util::max(), but using an array slice rather than splice:
(The call to max() could be replaced with a ?: ternary if necessary.)c:\@Work\Perl>perl -wMstrict -le "use List::Util qw(max); ;; my @ra = qw(a b c d e f g h); ;; my $n = 3; for my $i (0 .. $#ra) { my @triplet = @ra[ max(0, $i - $n + 1) .. $i ]; printf q{'%s' }, join '', @triplet; } " 'a' 'ab' 'abc' 'bcd' 'cde' 'def' 'efg' 'fgh'
Give a man a fish: <%-{-{-{-<
In reply to Re^5: Sub-setting an Array
by AnomalousMonk
in thread Sub-setting an Array
by kgherman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |