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"; #### chomp( my $mask_input = ); my $new_mask = 'N' x length $mask_input; ... while ( ) { my ( undef, $start, $end ) = split; --$start; substr $new_mask, $start, $end - $start, substr $mask_input, $start, $end - $start; } ... print OUT "$new_mask\n";