It seems to me that no searching is required, as all the relevant information already present in the input data.
A grossly over elaborate coding is:
#! perl -slw use strict; my $s1 = "1,0,CTGCCACCGCTGT"; my $s2 = "1,5,ACCGCTGTGTTTCGGCCGGCGA"; my @s1 = split ',', $s1; my @s2 = split ',', $s2; if( $s1[0] == $s2[0] ) { if( $s1[1] > $s2[1] ) { my @temp = @s1; @s1 = @s2; @s2 = @temp; } if( $s2[1] < length( $s1 ) ) { ## Updated to correct the error pointed out by Lima1++ below my $l1 = length( $s1[2] ) - $s1[1] - $s2[1]; my $l2 = length( $s2[2] ); $l1 = $l2 if $l2 < $l1; print "The intersection between the strings:\n", $s1[2], "\n", ' ' x ( $s2[1] - $s1[1] ), $s2[2], "\nis\n", ' ' x $s2[1], substr $s1[2], $s2[1] - $s1[1], $l1; ; print "\nand it occurs at a 1-based offset into the sequence o +f ", $s2[1] + 1; } else { print "The strings do not intersect"; } } else { print 'Strings are not from the same sequence'; } __END__ c:\test>junk The intersection between the strings: CTGCCACCGCTGT ACCGCTGTGTTTCGGCCGGCGA is ACCGCTGT and it occurs at a 1-based offset into the sequence of 6
The bits you are probably interested in are
substr $s1[2], $s2[ 1 ] - $s1[ 1 ], length( $s1[2] ) - $s1[1] - $s2[1] +;
$s2[1] + 1;
but they won't work correctly unless you split the input strings into their constituant parts and ensure that the first is the 'lesser' of the two.
It could undoubtedly be golfed some.
In reply to Re: Intersecting Two Strings
by BrowserUk
in thread Intersecting Two Strings
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |