Programming Perl

Programming PerlSearch this book
Previous: 3.2.74 importChapter 3
Functions
Next: 3.2.76 int
 

3.2.75 index

index STR, SUBSTR, POSITION
index STR, SUBSTR

This function returns the position of the first occurrence of SUBSTR in STR. The POSITION, if specified, says where to start looking. Positions are based at 0 (or whatever you've set the $[ variable to - but don't do that). If the substring is not found, the function returns one less than the base, ordinarily -1. To work your way through a string, you might say:

$pos = -1;
while (($pos = index($string, $lookfor, $pos)) > -1) {
    print "Found at $pos\n";
    $pos++;
}


Previous: 3.2.74 importProgramming PerlNext: 3.2.76 int
3.2.74 importBook Index3.2.76 int