>perl -wMstrict -le "my @ra = (0, 1, 42, 3, 4, 5, 6, 7, 8, 42, 10, 42, 12, 42); ;; for my $i (0 .. $#ra) { look_back_5_for_42(\@ra, $i); } ;; sub look_back_5_for_42 { my ($ar, $current_i) = @_; ;; my $back = $current_i - 5 + 1; $back = 0 if $back < 0; ;; printf qq{at index $current_i: }; for my $i ($back .. $current_i) { printf qq{42 at index $i, } if $ar->[$i] == 42; } print ''; } " at index 0: at index 1: at index 2: 42 at index 2, at index 3: 42 at index 2, at index 4: 42 at index 2, at index 5: 42 at index 2, at index 6: 42 at index 2, at index 7: at index 8: at index 9: 42 at index 9, at index 10: 42 at index 9, at index 11: 42 at index 9, 42 at index 11, at index 12: 42 at index 9, 42 at index 11, at index 13: 42 at index 9, 42 at index 11, 42 at index 13,