Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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??

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.


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


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1199349]
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
Find Nodes?
    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.