rindex Function in Perl
What is rindex Function in Perl?
Explanation
The rindex function is used to search occurence of a string from the left to right.
Syntax:
rindex STR,SUBSTR,POSITION
rindex STR,SUBSTR
In the above syntax rindex() function takes a "STR" specifies the string in which the index value of "SUBSTR" has to be found, the optional argument "POSITION" is used to specify the optional offset that is index till which to be checked.
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$str = 'hi how are you hayden';
$sstr1 = 'o';
$result2 = rindex($str,$sstr1);
print "Output without Offset: $result2n";
Result :
Output without Offset: 12
In the above example the offset value is not given, so by default the last occurence of 'o' is returns an index value of "12".
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$str = 'hi how are you hayden';
$sstr = 'h';
$pos = 7;
$result = rindex($str, $sstr,$pos);
print "Output with Offset: $resultn";
Result :
Output with Offset: 3
In the above rindex function example first the string "hi how are you hayden" is used to find the second last occurence of "h". We give an offset less than the last occurence of "h" that is "14", since the string is checked backwards from the offset position but the index value is evaluated from left to right.The offset given here is "7", so the rindex() function returns "3" as the index value, for the second last occurence of "h".