In this second part of a three-part article series on string processing in Perl, you will learn about the rindex() and substr() functions. This article is excerpted from, teh book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN-10: 159059391X)
The rindex() Function
The rindex()function is similar toindex()except that it searches the string from right to left (instead of left to right). The syntax is similar:
This function searches right-to-left through the string searching for the substring. It returns the 0-based index of where the substring is in the string, or –1 if the substring is not found.
An important note: even though this function searches through the string from right to left, the 0th character of the string is still the left-most character.
rindex('David Gilmour', 'i')
searches from the right-hand side of “David Gilmour” looking for the substring “i”. It finds it at position 7 (the “i” in “Gilmour”).
This function also has an optional third argument that is the character position from which it begins looking for the substring. This invocation:
rindex('David Gilmour', 'i', 6)
starts at position 6 (the “G” in “Gilmour”) and looks right to left for an “i” and finds it at position 3.