# Assuming $lastEnd is initialized to 0 above my $length = $start - $lastEnd; if ($length) { my $replacement = 'N' x $length; substr( $mask_input, $lastEnd, $length, $replacement ); }