in reply to Problem in "index" function in recognizing repeating substrings

Do you want:
my $str = "ATCGATCGAT"; my @substr = qw(ATCG CGAT);
to give 0 and 2 or 0 and 6?

If the latter, do

$id = -1; foreach (@substr) { my $id = index($str,$_,$id); print "$id\n"; $id += length($_); }

Replies are listed 'Best First'.
Re^2: Problem in "index" function in recognizing repeating substrings
by reasonablekeith (Deacon) on Sep 02, 2005 at 14:29 UTC
    I've pointed this out to him before, didn't get a response then either.
    ---
    my name's not Keith, and I'm not reasonable.
Re^2: Problem in "index" function in recognizing repeating substrings
by monkfan (Curate) on Sep 02, 2005 at 14:52 UTC
    Dear ysth,
    Thanks a lot for pointing out the important case that I missed.
    With your example above, I will opt for the 'closest' substrings namely:
    0 and 2
    If the latter
    You mean the 'former'? Because, having tested your above code, I think it already gave that answer..
    Please correct me if I'm wrong.

    Regards,
    Edward
      No, his code clearly gives 0 and 6. If you want 0 and 2, try this:
      #!/usr/bin/perl use strict; my $str = 'ATCGATCGAT'; my @sub = qw(ATCG CGAT); my $id = 0; foreach (@sub) { $id = index($str, $_, $id); print "$id\n"; $id++; }