Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Problem in "index" function in recognizing repeating substrings

by monkfan (Curate)
on Sep 02, 2005 at 09:30 UTC ( [id://488585]=perlquestion: print w/replies, xml ) Need Help??

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++; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://488585]
Approved by Tanalis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found