my $s1 = "STCATTNNNSATCGCT"; my $s2 = "ATCGTCGSNNNNATCG"; ## # 0 # # There are 4 matching positions - marked with (#) - # and notice that matching N is not counted (0) #### my $match = $s1 ^ $s2; my $match_count = $match =~ tr/\00//; print "$match_count\n"; # This gives 5, because N is counted. # How can I make it to return 4 instead? #### get_match_count($s1,$s2); sub get_match_count { my ( $k, $l ) = @_; return 0 if $k eq $l; my $len = length($k); my $num_match = 0; for ( my $i = 0; $i < $len; $i++ ) { next if ( substr( $k, $i, 1 ) eq 'N' and substr( $l, $i, 1 ) eq 'N' ); ++$num_match if substr( $k, $i, 1 ) eq substr( $l, $i, 1 ); } print "$num_match\n"; return $num_match; }