#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11121442 use warnings; my @data = qw( 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 ); my $target = 5; my @answers = map { $data[$_] == $target ? @data[$_ + 1 .. $_ + 3] : () } 0 .. $#data; print "@answers\n"; #### 6 7 8 4 3 2