monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Most revered monks,
The built-in index function seems to be able to recognize only the first substring given two identical substrings from a string. My code below:
#!/usr/bin/perl -w use strict; my $str = 'NNNNNATCGNNNNATCG'; my @substr = qw(ATCG ATCG); foreach (@substr) { my $id = index($str,$_); print "$id\n"; }
Returns:
5 5
How can I overcome this problem such that the index function can recognize the second substrings in the correct position? Returning this instead:
5 13

Regards,
Edward

Replies are listed 'Best First'.
Re: Problem in "index" function in recognizing repeating substrings
by reasonablekeith (Deacon) on Sep 02, 2005 at 09:47 UTC
    #!/usr/bin/perl -w use strict; my $str = 'NNNNNATCGNNNNATCG'; my @substr = qw(ATCG ATCG); my $id = -1; foreach (@substr) { $id = index($str, $_, $id+1); print "$id\n"; }
    ---
    my name's not Keith, and I'm not reasonable.
Re: Problem in "index" function in recognizing repeating substrings
by Tanalis (Curate) on Sep 02, 2005 at 09:35 UTC
    index accepts a position to start from as a third argument. If you track $id to figure out where in the string you are, you should be able to avoid matching the first instance repeatedly.

    See the docs for more information.

Re: Problem in "index" function in recognizing repeating substrings
by ysth (Canon) on Sep 02, 2005 at 12:00 UTC
    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($_); }
      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.
      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++; }