Your skill will accomplishwhat the force of many cannot PerlMonks

### what is the difference between index and rindex

by jesuashok (Curate)
 on Sep 05, 2006 at 09:45 UTC Need Help??

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
• Comment on what is the difference between index and rindex

Replies are listed 'Best First'.
Re: what is the difference between index and rindex
by Skeeve (Parson) on Sep 05, 2006 at 09:52 UTC
perldoc -f rindex

s\$\$([},&%#}/&/]+}%&{})*;#\$&&s&&\$^X.(\$'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`\$''`"e
Re: what is the difference between index and rindex
by reasonablekeith (Deacon) on Sep 05, 2006 at 10:00 UTC
jesuashok, you've been an active member here for long enough, you should know better than to ask a question that can be so easily answered by reading the documentation or a quick bit of testing.

It may sound grumpy, but questions like this just fill perlmonks with noise. I'm afraid it's a -- from me, and a ++ to your first reply.

---
my name's not Keith, and I'm not reasonable.
A reply falls below the community's threshold of quality. You may see it by logging in.
Re: what is the difference between index and rindex
by gellyfish (Monsignor) on Sep 05, 2006 at 09:52 UTC

One starts searching at the beginning of the string and the other does the search in reverse, hence the 'r'

/J\

Hmm, I always thought the "r" stood for "right". As in BASIC (the functions Left\$, Mid\$, Right\$ — Microsoft's DOS based QuickBASIC had them).

What was really cool is that MID\$ was an lvalue and, if I remember correctly, could be assigned a string of a different length from its present value, i.e., you could replace n characters in the middle of a string with m new characters. Before I discovered Perl, I used to think QBasic was *the* language for string manipulation and text-munging jobs. (Of course, these days lots of languages can do that stuff. But back then, Borland C++ was the hot new thing, and it didn't have *anything* like MID\$.) ISTR that LEFT\$ and RIGHT\$ had these properties too, but MID\$ was the most general and therefore the most useful.

Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. You can just call me "Mister Sanity". Why, I've got so much sanity it's driving me crazy.
Re: what is the difference between index and rindex
by ikegami (Patriarch) on Sep 05, 2006 at 16:43 UTC

Sometimes, an example works best.

```>perl -e "print index 'abcdcda', 'c'"
2
>perl -e "print rindex 'abcdcda', 'c'"
4

Reference: index, rindex

Re: what is the difference between index and rindex
by mreece (Friar) on Sep 05, 2006 at 20:53 UTC
fwiw, rindex appears to be slightly faster:
```use Benchmark qw(timethese);

my \$s = "abcdefg";

timethese( 6_000_000,
{
rindex => sub { my \$i = rindex( \$s, "cde" ) },
index => sub { my \$i =  index( \$s, "cde" ) },
}
);
Benchmark: timing 6000000 iterations of index, rindex...
index: 1 wallclock secs ( 1.14 usr + 0.00 sys = 1.14 CPU) @ 5263157.89/s (n=6000000)
rindex: 1 wallclock secs ( 1.02 usr + 0.00 sys = 1.02 CPU) @ 5882352.94/s (n=6000000)
their functional difference is that rindex starts from the end of the string, and index starts at the beginning. there is no difference in their usage.

For me, index is faster. In any case the difference is less than 10% (for both me and you), so the test is not conclusive. ( Even if it were conclusive, the difference is rather minor. Of course, this is all moot since the two functions are not interchangeable. )

A reply falls below the community's threshold of quality. You may see it by logging in.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://571220]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2022-01-28 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2022, my preferred method to securely store passwords is:

Results (73 votes). Check out past polls.

Notices?