perl count mismatch between two strings -


i need count mismatch between 2 strings. let say:

my $s1 = "atcg"; $s2 = "attg";  

this should give: 1 mismatch. no need find position or mismatches.

i looking fast way do. thought splitting arrays , matching in loop or using substr match each position may slow because need checked more billion pairs.

just xor 2 strings together. each nul character in result represents position characters same in both strings.

my ($s1, $s2) = qw( atcg attg );  $count = ( $s1 ^ $s2 ) =~ tr/\0//c;  print "$count\n";   # prints "1" 

note: if you're going repeatedly compare string, pass , 1 comparing utf8::downgrade makes sure ^ fast can be.

utf8::downgrade($s1);  # change internal format in utf8::downgrade($s2);  #   strings stored speed $s1^$s2. 

this useless/wasteful if either string contains unicode chars above u+00ff.


Comments

Popular posts from this blog

html - Difficulties with background-image property -

visual studio code - What does the isShellCommand property actually do and how should you use it? -

ios - Segue not passing data between ViewControllers -