Perl rindex Function



You may consider the Perl rindex function like a reverse for index function – right to left substring search. It returns the position of the last occurrence of a substring in a string, looking from right to left. In this tutorial we’ll show how to use this function in your script applications through several examples.

To find a substring in a string from right to left, you can use the regular expressions too, but using Perl rindex function is faster and easier to understand. Try to avoid using regular expressions for simple string operations especially when some built-in functions specially designed for this purpose are available. The regex have to manipulate memory variables, so keep them for more sophisticated and complex operations.

Before beginning with, you can skim a bit through Perl index page in order to better understand the topic discussed here.


Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!


Two syntax forms are available for this function:

rindex STR,SUBSTR,POSITION 
rindex STR,SUBSTR

Here, the three arguments have the following meaning:
  • STR - the string you are searching
  • SUBSTR - the substring whose position you want to locate or to search for
  • POSITION – if specified, represents the rightmost position that can be returned, from that position the function will search from right to left looking for the substring to match
If you don’t provide the third argument, the Perl rindex function will search for the substring beginning with the last character of the string and from right to left. It will return the position of the first occurrence of the substring if the substring was found and -1 otherwise. The parentheses are optional, it is your choice to use them or not. Still, there are cases when it is better to use them, in order to make your code more readable.

Please examine the code below to see some simple examples about how to use the Perl rindex function:

# initialize a scalar string variable
my $str = "Perl while Perl if Perl foreach";
my $substr = "Perl";
my $pos = rindex $str, $substr;
my $pos1 = rindex($str, $substr, 15); 
print "\$pos = $pos, \$pos1 = $pos1\n";
# it displays pos = 19, $pos1 = 11

It works just like index function except that it scans the string from right to left and returns the position of the last occurrence of the substring, there is no other difference in their usage.

Check if a substring is included in a string


We can use the return value of the Perl rindex function to find out if a substring is included in a string, looking backward, i.e. from write to left. You can also supply a start position for our searching or let the rindex function search from the rightmost character of the string.

If the substring is not found, rindex returns -1, otherwise it returns the last position of the occurrence of the substring.

# initialize a string scalar
my $textStr = "PDF::Create is a Perl module";
if((my $pos = rindex(lc $textStr, lc "perl")) >= 0)
{
  print "We found it!\n";
}

Please note that at first we converted the string and the substring in lowercase (by using lc function) in order to make our search case insensitive.

Find out all the occurrences of a substring in a string


You can search for all the occurrences of a substring in a string either by using index or rindex function. The example below looks for the positions of the slash character in a string (you can use the same algorithm to search for a substring composed from more than one characters, though).

# initialize a string scalar
my $url = "http://www.mysite.com/perl-rindex.html";
my $pos = length($url)-1;

print "/ found at positions: ";

while(1)
{
  $pos = rindex($url, "/", $pos);
  last if($pos < 0);
  print $pos--, " ";
}

print "\n";

We used the last keyword to finish the while loop iteration. If you run this code, you’ll get the output:

/ found at positions: 29 6 5

Please click here to download the Perl rindex script with all the above examples included.

A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!


Check these how-to tutorial eBooks (PDF format):


Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        delete
        each
        exists
        grep
        hex
        index
        join
        keys
        lc
        lcfirst
        length
        map
        oct
        ord
        pack
        pop
        push
        q
        qq
        qw
        reverse
        rindex (more)
        scalar
        shift
        sort
        splice
        split
        sprintf
        substr
        tr
        uc
        ucfirst
        undef
        unpack
        unshift
        values

return from Perl rindex Function to Perl Basics



Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!