my $mask_input = ; ... my $lastEnd = 1; while ( ) { my (undef, $start, $end) = split '\s', $_; ## change everything from the end of the last range ## to the start of this range to 'N' substr( $mask_input, $lastEnd, $start ) =~ tr[\x00-\xff][N]; $lastEnd = $end; } ... print OUT "$mask_input";