index function problem

Discussion in 'Perl Misc' started by shadbo, Feb 5, 2004.

  1. shadbo

    shadbo Guest

    I am a perl newbie and I seem to be getting incorrect results from the
    index function.

    I have a var $string_to_find, which has a value of 'paris'. The string
    I am searching has a 'paris' in there and 'pari' I want to extract the
    position of 'paris' and not 'pari'. The code below seems to be finding
    the position of the first one (pari), which i don't need.
    Is it possible to combine regex with the index funtion so it only
    searches for 'paris'?

    $string_position = index($line,$string_to_find)

    Any suggestions would be greatly appreciated.

    Thanks
     
    shadbo, Feb 5, 2004
    #1
    1. Advertising

  2. shadbo

    Paul Lalli Guest

    On Thu, 5 Feb 2004, shadbo wrote:

    > I am a perl newbie and I seem to be getting incorrect results from the
    > index function.
    >
    > I have a var $string_to_find, which has a value of 'paris'. The string
    > I am searching has a 'paris' in there and 'pari' I want to extract the
    > position of 'paris' and not 'pari'. The code below seems to be finding
    > the position of the first one (pari), which i don't need.
    > Is it possible to combine regex with the index funtion so it only
    > searches for 'paris'?
    >
    > $string_position = index($line,$string_to_find)
    >
    > Any suggestions would be greatly appreciated.



    First suggestion would be to post your code so we can figure out what
    you're doing wrong. . . .

    Paul Lalli
     
    Paul Lalli, Feb 5, 2004
    #2
    1. Advertising

  3. Paul Lalli wrote:
    > On Thu, 5 Feb 2004, shadbo wrote:
    >
    >
    >>I am a perl newbie and I seem to be getting incorrect results from the
    >>index function.
    >>
    >>I have a var $string_to_find, which has a value of 'paris'. The string
    >>I am searching has a 'paris' in there and 'pari' I want to extract the
    >>position of 'paris' and not 'pari'. The code below seems to be finding
    >>the position of the first one (pari), which i don't need.
    >>Is it possible to combine regex with the index funtion so it only
    >>searches for 'paris'?
    >>
    >>$string_position = index($line,$string_to_find)
    >>
    >>Any suggestions would be greatly appreciated.

    >
    >
    >
    > First suggestion would be to post your code so we can figure out what
    > you're doing wrong. . . .


    Five will get you ten he's using chop() instead of chomp()
    on $string_to_find--and $string_to_find didn't have a \n
    at the end.

    Chris Mattern
     
    Chris Mattern, Feb 5, 2004
    #3
  4. shadbo <> wrote:

    > The code below seems to be finding
    > the position of the first one (pari), which i don't need.


    > $string_position = index($line,$string_to_find)



    index() will find the first one.

    rindex() will find the last one.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 5, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karthikeyavenkat
    Replies:
    2
    Views:
    598
    Bryce
    Mar 17, 2005
  2. Shawn W_
    Replies:
    5
    Views:
    285
    Aldric Giacomoni
    Sep 16, 2009
  3. ngoc
    Replies:
    5
    Views:
    177
    Tad McClellan
    May 11, 2006
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    300
    Tomasz Chmielewski
    Mar 4, 2008
  5. Stefan Mueller
    Replies:
    7
    Views:
    189
    Thomas 'PointedEars' Lahn
    Nov 28, 2005
Loading...

Share This Page