in reply to Re: match and mismatch
in thread match and mismatch
The tr/// changes non-zero bytes to x. The while loop then uses index to search through the difference string for the x bytes and reports their index (0 based position).
I think it might be simpler to use a global regex match for non-nulls and pos. You can just match them for 1-based counting or put them in a look-ahead for 0-based counting. Note that I added another difference in the third characters to check that it was working as I had hoped.
use strict; use warnings; my $one = "AGCTGATCGAGCTAGTACCCTAGCTC"; my $two = "AGATGATCGAGCTAGTACCCTATCTC"; # Diffs here ^ ^ my $diff = $one ^ $two; my @posns = (); push @posns, pos $diff while $diff =~ m{[^\0]}g; print qq{Differences, 1-based counting, at - @posns\n}; @posns = (); push @posns, pos $diff while $diff =~ m{(?=[^\0])}g; print qq{Differences, 0-based counting, at - @posns\n};
The output.
Differences, 1-based counting, at - 3 23 Differences, 0-based counting, at - 2 22
I hope this is of interest.
Cheers,
JohnGG
|
---|