Basic HTML version of Foils prepared Sept 20 97

Foil 50 The index and rindex Functions

From Tutorial on PERL Computational Science for Information Age Course CPS616 -- Sept 20 97. by Geoffrey C. Fox,Nancy McCracken,Tom Scavo


1 $loc = index($string, $substr); # returns in $loc the location (first character in $string is location 0) of first occurrence of $substr in $string.
  • If $substr is not found, index returns -1
2 $loc = index($string, $substr, $firstloc); # returns $loc which is at least as large as $firstloc
  • Use to find multiple occurrences, setting $firstloc as 1+ previously found location
3 rindex($string, $substr, $lastloc) is identical to index except scanning starts at right (end) of string and not at start. All locations still count from left but if you give a third argument $lastloc, the returned $loc will be at most $lastloc

in Table To:


© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Sep 21 1997