my ($min,$max) = (0,21); my @nums = (1, 5, 6, 8, 15, 20); my $pad = 1; my %line; map { $line{$_}++ for ($_-$pad..$_+$pad) } @nums; my @padded = sort { $a <=> $b } keys %line; # print the lines for (@padded){ if (@nums && $_ == $nums[0]){ print "Hilighted: $_\n"; shift @nums; } elsif ($min <= $_ && $_ <= $max) { print "$_\n"; } } #### 0 Hilighted: 1 2 4 Hilighted: 5 Hilighted: 6 7 Hilighted: 8 9 14 Hilighted: 15 16 19 Hilighted: 20 21