Home
FREE Newsletter
Perl How To
Perl Books
Perl Basics
Perl Editors
Perl Modules
Perl Database
Net Programming
Sitemap
About Me
Contact Me
Blog
 

Perl rindex Function



NEW!!!

Check my new resource:    Perl How To Tutorial eBooks



You may consider 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 regexp 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.


Check my new How To Tutorial eBooks (PDF format):

to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.



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 backword, i.e. from write to left. We 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


We 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 slash character in a string ( we 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 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.



Subscribe now to my FREE newsletter
(regarding tips and tricks to manipulate multidimensional
arrays and hashes in Perl)

and you'll receive a link to download for FREE my Perl chomp Function Tutorial eBook (a $7.50 value), it's a bonus for you and I hope you'll enjoy it. You can see its Table of Contents here (if you subscribe to my free newsletter, you don't have to pay for this eBook or use your paypal account).


Don't forget to check my new How To Tutorial eBooks (PDF format):

to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.






NEW!!!

Do you want more information about the basic Perl topics?

Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :


Perl How To Tutorial eBooks


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
        Array Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        grep
        hex
        index
        join
        lc
        lcfirst
        length
        map
        oct
        ord
        pack
        pop
        push
        qw
        reverse
        rindex
        scalar
        shift
        sort
        splice
        split
        substr
        uc
        undef
        unshift

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!


footer for perl rindex page