I am trying to put the bracket in a string given the set of its substrings. Like this:
String Substrings Desired Result
1.CCCATCTGTCCTTATTTGCTG ATCTG ATTTG CCC[ATCTG]TCCTT[ATTTG]CTG
2.ACCCATCTGTCCTTGGCCAT CCATC AC[CCATC]TGTCCTTGGCCAT
3.CCACCAGCACCTGTC CCACC CCAGC GCACC [CCACCAGCACC]TGTC *
4.CCCAACACCTGCTGCCT CCAAC ACACC C[CCAACACC]TGCTGCCT *
My code here does the job, but only
for the first two cases (where the substrings don't overlap in the string). It fails to deal with case 3 and 4 (marked with asterisks *).
my $s1 ='CCCATCTGTCCTTATTTGCTG'; my @a1 = qw(ATCTG ATTTG);
my $s2 ='ACCCATCTGTCCTTGGCCAT'; my @a2 = qw(CCATC);
my $s3 ='CCACCAGCACCTGTC'; my @a3 = qw(CCACC CCAGC GCACC);
my $s4 ='CCCAACACCTGCTGCCT'; my @a4 = qw(CCAAC ACACC);
#These two work fine.
#But these two don't work
my ($str,$ar) = @_;
my $slen = length $ar->;
foreach my $subs ( @$ar )
my $idx = index($str,$subs);
my $bgn = $idx;
my $end = $idx+$slen+1;
How can I resolve this problem?
Am I approaching the problem correctly in the first place?
I humbly seek your advice.