http://www.perlmonks.org?node_id=1199349


in reply to Re^2: How do you match a stretch of at least N characters
in thread How do you match a stretch of at least N characters

Is this what you were looking for? The longest matching DNA string? You can save the max length found below to get your result.

Answer:

Found match of length (102) at position(506, 0):

AAATTGGTGTATATGAAAGACCTCGACGCTATTTAGAAAGAGAGAGCAATATTTCAAGAATGCATGCGTCAATTTTACGCAGACTATCTTTCTAGGGTTAAT
AAATTGGTGTATATGAAAGACCTCGACGCTATTTAGAAAGAGAGAGCAATATTTCAAGAATGCATGCGTCAATTTTACGCAGACTATCTTTCTAGGGTTAAA

my $i=-1; while(++$i < length($reference_str)) { my $j=-1; while(++$j < length($small_str)) { my $length = 9; my $match=undef; while ( $i+$length < length($reference_str) and $j+$length < length($small_str) and compareSnippet($i, $j, ++$length) ) { $match++; } if ( $match) { $length--; print "Found match of length ($length) at position($i, $j): +\n"; print substr($reference_str, $i, $length) . "\n"; print substr($small_str, $j, $length). "\n"; $i+= $length; $j+= $length; next; } } } sub compareSnippet { my ( $pos_i, $pos_j, $length) = @_; my $ref = substr($reference_str, $pos_i, $length); my @ref_arr = sp +lit('', $ref); my $small = substr($small_str, $pos_j, $length); my @small_arr = sp +lit('', $small); return undef if length($ref) < $length or length($small) < $length; return equal(\@ref_arr, \@small_arr); } sub equal { my ($first, $second) = @_; my $mismatch=0; foreach my $i (0..(@$first-1)) { $mismatch++ if $first->[$i] ne $second->[$i]; return undef if $mismatch > 1; } # print "Woo! (@$first, @$second)\n" if $mismatch == 0; return 1; }