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"; } }