Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

what is the difference between index and rindex

by jesuashok (Curate)
on Sep 05, 2006 at 09:45 UTC ( #571220=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

dear fellows,

what is the difference between index and rindex in perl. could any one tell me the difference of these two in all perspectives of perl.

* difference in their performance
* functional difference
* usage difference
etc ... etc.....

"Keep pouring your ideas"

Comment on what is the difference between index and rindex
Re: what is the difference between index and rindex
by Skeeve (Vicar) on Sep 05, 2006 at 09:52 UTC
    perldoc -f rindex

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'


      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 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.
Re: what is the difference between index and rindex
by jesuashok (Curate) on Sep 05, 2006 at 10:05 UTC
    dear monks,

    I applogise to ask this question in perlmonks. one of my junior who was accessing my PC in the absence of me asked this question. I am very much familiar with index and rindex. please don't reply to this node. I am very sorry for the inconvenience which caused by my junior.

    "Keep pouring your ideas"
      I am not sure who "my junior" is. Even if it is your son, you should probably set him up with his own account and teach him the importance of security by locking your machine when you are away. If this is a junior co-worker, then you are really setting a bad example. I don't know what your company's policy is on sharing passwords but here it is strictly prohibited. By policy, you must lock your machine even if you are only running to pick something up off the printer.

      Cheers - L~R

      Funny to see how you and... your junior... type in the same way.
      • dear <something>, -- no capitalisation
      • no capital letters after periods
      • the word I is always capitalised

      And you expect us to believe that?

      Nice :)
Re: what is the difference between index and rindex
by ikegami (Pope) 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. )

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://571220]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (16)
As of 2014-07-22 19:52 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (126 votes), past polls