#! 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 of ",
$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
####
substr $s1[2], $s2[ 1 ] - $s1[ 1 ], length( $s1[2] ) - $s1[1] - $s2[1];
####
$s2[1] + 1;