my $max = 21; # In the "real world" I obtain these line IDs from a db query, and do # the next step (generate @padded_overlap) on the fly my @nums = (1, 5, 6, 8, 15, 20); my $pad = 1; # lines of context on each side my @padded_overlap = map { $_ - $pad .. $_ + $pad } @nums; # guard against context from next/previous day shift @padded_overlap while ($padded_overlap[0] < 0); pop @padded_overlap while ($padded_overlap[-1] > $max); # remove duplicates my $prev = shift @padded_overlap; my @padded = ($prev); for (@padded_overlap) { if ($_ > $prev){ push @padded, $_; $prev = $_; } } # print the lines for (@padded){ if (@nums && $_ == $nums[0]){ print "Hilighted: $_\n"; shift @nums; } else { print "$_\n"; } }