Perl rindex Function



Description

This function operates similar to index, except it returns the position of the last occurrence of SUBSTR in STR. If POSITION is specified, returns the last occurrence at or before that position.

Syntax

Following is the simple syntax for this function −

rindex STR, SUBSTR, POSITION

rindex STR, SUBSTR

Return Value

This function returns undef on failure otherwise position of last occurence.

Example

Following is the example code showing its basic usage −

#!/usr/bin/perl -w

$pos = rindex("abcdefghijiklmdef", "def");
print "Found position of def $pos\n";

# Use the first position found as the offset to the
# next search.
# Note that the length of the target string is
# subtracted from the offset to save time.

$pos = rindex("abcdefghijiklmdef", "def", $pos-3 );
print "Found position of def $pos\n";

When above code is executed, it produces the following result −

Found position of def 14
Found position of def 3
perl_function_references.htm
Advertisements