#!/usr/bin/perl use warnings; use strict; use List::Util; package main; my $max = 21; my @nums = (1, 5, 6, 8, 15, 20); my $pad = 1; # lines of context on each side my $last = -1 - $pad; foreach ( @nums ) { my $start = List::Util::max 1, $_ - $pad, $last + 1; my $end = List::Util::min $max - 1 , $_ + $pad; next if ( $start > $end ); for ( my $i = $start; $i <= $end; $i++ ) { if ( $i == $_ ) { print "Highlighted $i\n"; } else { print "Line $i\n"; } } $last = $end; }