use strict; use warnings; my @cache; my $n_min=2; my $n_max=6; while (my $line =) { chomp $line; push @cache, $line; for my $n ($n_min..$n_max) { print "$n: @cache[-$n .. -1]\n" # last n of cache if @cache >= $n; } shift @cache if @cache == $n_max; # keep cache at max size } __DATA__ One Two Three Four Five Six Seven Eight Nine Ten