perl -E 'say rindex("bar foo bar foo", "foo")'
                                 ^__ found first substring from right side
12

perl -E 'say index("bar foo bar foo", "foo")'
                        ^__ found first substring from left side
4
Answer from mpapec on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › perl › perl-rindex-function
Perl | rindex() Function - GeeksforGeeks
July 11, 2025 - Syntax: # Searches pat in text from given Position rindex text, pattern, Position # Searches pat in text rindex text, pattern Parameters: text: String in which substring is to be searched. ... index: Starting index(set by the user or it takes zero by default). Returns: -1 on failure otherwise position of last occurrence. Example 1: ... #!/usr/bin/perl -w $pos = rindex("WelcomeToGeeksforGeeksWorld", "eks"); print "Position of eks: $pos\n"; # Use the first position found as the offset # to the next search.
🌐
Perl Documentation
perldoc.perl.org › functions › rindex
rindex - Perldoc Browser
#rindex STR,SUBSTR · Works just like index except that it returns the position of the last occurrence of SUBSTR in STR. If POSITION is specified, returns the last occurrence beginning at or before that position. Perldoc Browser is maintained by Dan Book (DBOOK).
🌐
TutorialsPoint
tutorialspoint.com › perl › perl_rindex.htm
Perl rindex Function
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.
🌐
ZetCode
zetcode.com › perl › rindex
Perl rindex - Finding Substrings from the Right in Perl
April 4, 2025 - The Perl rindex function searches for a substring from the end of a string. It returns the position of the last occurrence or -1 if not found. rindex is similar to index but searches right to left.
🌐
Softpanorama
softpanorama.org › Scripting › Perlorama › Functions › String_operations_in_perl › index_and_rindex.shtml
Perl index and rindex built-in functions
The index function search its first operand (string) in the second operand (substring) and return the offset of the first substring found. The rindex function returns the offset of the last substring found.
🌐
SDSU
edoras.sdsu.edu › doc › perldoc-html › functions › rindex.html
rindex - perldoc.perl.org
Perl functions A-Z | Perl functions by category | The 'perlfunc' manpage · rindex STR,SUBSTR,POSITION · rindex STR,SUBSTR · Works just like index() except that it returns the position of the LAST occurrence of SUBSTR in STR. If POSITION is specified, returns the last occurrence at or before ...
🌐
GeeksforGeeks
geeksforgeeks.org › perl › perl-string-functions-length-lc-uc-index-rindex
Perl | String functions (length, lc, uc, index, rindex) - GeeksforGeeks
June 26, 2019 - It searches from the end of the string instead of from the beginning. Below are the programs to illustrate this method. Example 1: ... # Perl Program to illustrate # the rindex() function # !/usr/bin/perl use warnings; use strict; # string my $st = "GeeksforGeeks\n"; # substring my $subs = "for"; # using rindex function my $r = rindex($st, $subs); # displaying result print(qq\The substring $subs found at position $r in string $st\); Output:
🌐
Hardware Haven
perlmeme.org › home › blog › howtos
HOWTOs - Hardware Haven
December 12, 2024 - The focus of this section is providing in-depth coverage of a part of Perl. Solutions are provided that illustrate the […]
Find elsewhere
🌐
HScripts
hscripts.com › tutorials › perl › functionforstrings › rindex.php
rindex Function - Perl String Tutorials
Sign in / Sign up · Home · >> Tutorials · >> Perl · >> String Functions · >> rindex Function in Perl · What is rindex Function in Perl? The rindex function is used to search occurence of a string from the left to right.
🌐
Perl Maven
perlmaven.com › rindex
rindex
use strict; use warnings; use 5.010; ... $str, "e", 34; # 34 say rindex $str, "e", 33; # 29 · It is like the index function but starts searching from the right-hand side....
🌐
Ub
cs.ub.bw › teaching › teachings › csi223 › perldoc-html-bak › functions › rindex.html
rindex - perldoc.perl.org
Perl functions A-Z | Perl functions by category | The 'perlfunc' manpage · rindex STR,SUBSTR,POSITION · rindex STR,SUBSTR · Works just like index() except that it returns the position of the last occurrence of SUBSTR in STR. If POSITION is specified, returns the last occurrence beginning ...
🌐
Perlzemi
en.perlzemi.com › builtin functions
rindex function - Search for a string from the end - Perl ABC - Perl Installation, Perl Tutorial, Many Examples
Perl · › · builtin functions · › · here · Use the rindex function to search for a string from the end of the string . my $pos = rindex($target, $str); index function searches for a string from the beginning, while the rindex function searches for a string from the end.
🌐
Perl Maven
perlmaven.com › string-functions-length-lc-uc-index-substr
String functions: length, lc, uc, index, substr
use strict; use warnings; use 5.010; ... there is another function called rindex (right index) that will start searching from the right hand side of the string:...
Top answer
1 of 2
9

This bug was fixed and will be in 5.22.0 which should be out in May. It didn't make it into the 5.20.x series. That patch is simple and you should be able to apply it to the 5.20.2 or 5.18.2 code base and recompile.

Regexes have the same problem, and it's also been fixed in 5.21.

The work around is to not have 2 gig strings in memory, that's good practice in general. If it's read from a file, perhaps read it in blocks and use index on each block.

If you must have a 2 gig string, use substr() to check it in blocks. Unfortunately this means you have to copy the string in pieces. The code below deliberately initializes $substr outside the loop so Perl doesn't reallocate the memory multiple times.

use v5.18;
use strict;
use warnings;

sub big_index {
    my $ref = shift;
    my $match = shift;

    state $block_size = 2**29;

    my $strlen   = length($$ref);
    my $matchlen = length($match);

    # No point in doing extra work if we don't need to.
    return index($$ref, $match) if $strlen < $block_size;

    my $substr = '';
    my $offset = 0;
    for(
        my $offset = 0;
        $offset < $strlen;
        $offset += ($block_size - $matchlen - 1)
    ) {
        $substr = substr($$ref, $offset, $block_size);
        my $ret = index $substr, $match;
        return $ret + $offset if $ret != -1;
    }

    return -1;
}
2 of 2
0

Thanks to @Schwern for the basic code. This version avoids copying in the 2 GB string by using substr directly. It supports the additional $position parameter like index. I also did the rindex version.

Call like index/rindex except call with a reference to to source string:

index64(\$string, $match);
index64(\$string, $match, $position);
rindex64(\$string, $match);
rindex64(\$string, $match, $position);


sub index64 {
    # Do index on strings > 2GB.
    # index in Perl < v5.22 does not work for > 2GB
    # Input:
    #   as index
    # Output:
    #   as index
    my $ref = shift;
    my $match = shift;
    my $pos = shift || 0;
    my $block_size = 2**31-1;
    my $strlen   = length($$ref);
    # No point in doing extra work if we don't need to.
    if($strlen < $block_size) {
        return index($$ref, $match, $pos);
    }

    my $matchlen = length($match);
    my $ret;
    my $offset = $pos;
    while($offset < $strlen) {
        $ret = index(
            substr($$ref, $offset, $block_size),
            $match, $pos-$offset);
        if($ret != -1) {
            return $ret + $offset;
        }
        $offset += ($block_size - $matchlen - 1);
    }
    return -1;
}

sub rindex64 {
    # Do rindex on strings > 2GB.
    # rindex in Perl < v5.22 does not work for > 2GB
    # Input:
    #   as rindex
    # Output:
    #   as rindex
    my $ref = shift;
    my $match = shift;
    my $pos = shift;
    my $block_size = 2**31-1;
    my $strlen = length($$ref);
    # Default: search from end
    $pos = defined $pos ? $pos : $strlen;
    # No point in doing extra work if we don't need to.
    if($strlen < $block_size) {
        return rindex($$ref, $match, $pos);
    }

    my $matchlen = length($match);
    my $ret;
    my $offset = $pos - $block_size + $matchlen;
    if($offset < 0) {
        # The offset is less than a $block_size
        # Set the $offset to 0 and
        # Adjust block_size accordingly
        $block_size = $block_size + $offset;
        $offset = 0;
    }
    while($offset >= 0) {
        $ret = rindex(
            substr($$ref, $offset, $block_size),
            $match);
        if($ret != -1) {
            return $ret + $offset;
        }
        $offset -= ($block_size - $matchlen - 1);
    }
    return -1;
}
🌐
Tjhsst
tjhsst.edu › ~dhyatt › perl › ex7.html
Perl Example #7 Working with Strings and Substrings
Perl is great at manipulating strings, naturally. The first ing begins at character 26 The last ing begins at character 33 · # Using the optional third parameter to "index" # Commmand Format: $x = index($bigstring, $littlestring, $skip); # Commmand Format: $x = rindex($bigstring, $littlestring, $before); $word = "at"; $first = index($sentence, $word); $last = rindex($sentence, $word); print "$sentence \n"; print "The index of the first $word is $first and the final index is $last\n"; $next = index( $sentence, $word, $first+1); print "After $first characters, the index of the next $word is $next \n"; $previous = rindex( $sentence, $word, $last-1); print "After $last characters, the index of the previous $word is $previous \n\n";
🌐
O'Reilly
oreilly.com › library › view › programming-perl-4th › 9781449321451 › ch27s02s140.html
rindex - Programming Perl, 4th Edition [Book]
February 17, 2012 - This function works just like index except that it returns the position of the last occurrence of SUBSTR in STR (a reverse index). The function returns –1 if SUBSTR is not found.
Authors: Tom Christiansenbrian d foyLarry WallJon Orwant
Published: 2012
Pages: 1184