I've used code like:

    #!/usr/bin/perl -w
    use strict;
    
    my $str = 'NNNNNATCGNNNNATCG';
    my @substr  = qw(ATCG ATCG);
    my $id = -1;
    foreach (@substr)
    {
       $id = index($str, $_, $id);
       print "$id\n";

       $id += length($_);
    }

before, but just now noticed that index doesn't document how it handles
a position outside the string to search.

--- perl/pod/perlfunc.pod.orig  2005-08-30 12:23:17.000000000 -0700
+++ perl/pod/perlfunc.pod       2005-09-02 05:12:10.841292800 -0700
@@ -2339,7 +2339,9 @@ =item index STR,SUBSTR
 the wildcard-like behavior of a full regular-expression pattern match.
 It returns the position of the first occurrence of SUBSTR in STR at
 or after POSITION.  If POSITION is omitted, starts searching from the
-beginning of the string.  The return value is based at C<0> (or whatever
+beginning of the string.  POSITION before the beginning of the string
+or after its end is treated as if it were the beginning or the end,
+respectively.  POSITION and the return value are based at C<0> (or whatever
 you've set the C<$[> variable to--but don't do that).  If the substring
 is not found, C<index> returns one less than the base, ordinarily C<-1>.
 
>From the code, this treatment of the end is intentional, though somewhat
surprising.  For instance, index("abc", "", 42) returns 3, not -1.

Reply via email to