more useful options PerlMonks

### Re^3: How do you match a stretch of at least N characters

by paisani (Acolyte)
 on Sep 13, 2017 at 20:43 UTC ( #1199349=note: print w/replies, xml ) Need Help??

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

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;
}

Create A New User
Node Status?
node history
Node Type: note [id://1199349]
help
Chatterbox?
 talexb is happy to be on this site.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2018-07-19 14:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

Results (408 votes). Check out past polls.

Notices?