for( my $i = 0; $i <= $#lines; $i++ ) { if ( $lines[$i] =~ s/^SY(?= )/CN/ ) { if ( defined $lines[$i+1] ) { $lines[$i+1] =~ s/^SY(?= )/ /; } } }