Rindex used to find end of a string

Discussion in 'Perl Misc' started by Paul.Lee.1971, Feb 28, 2007.

  1. Hi all,
    I have a sequence of strings, for instance
    "Hello, World "
    "My First Perl Test "
    "Exit(0) "

    - as you can see, the strings are all different lengths. Is there a
    way to use rindex to find out the last non-whitespace character in the
    string? I'd like to be able to use a general function to return
    "Hello, World", "My First Perl Test" and "Exit(0).

    Thanks in advance

    Paul
     
    Paul.Lee.1971, Feb 28, 2007
    #1
    1. Advertising

  2. Paul.Lee.1971

    Ben Morrow Guest

    Quoth "Paul.Lee.1971" <>:
    > Hi all,
    > I have a sequence of strings, for instance
    > "Hello, World "
    > "My First Perl Test "
    > "Exit(0) "
    >
    > - as you can see, the strings are all different lengths. Is there a
    > way to use rindex to find out the last non-whitespace character in the
    > string?


    Not easily. For a task like this you want a regex. The index of the last
    non-whitespace character in a string $str can be found with

    $str =~ /(\s*)$/ && length($str) - length($1) - 1;

    ....

    > I'd like to be able to use a general function to return
    > "Hello, World", "My First Perl Test" and "Exit(0).


    ....but if you just want to remove it you'd be better off doing that in
    one step:

    $str =~ s/\s*$//;

    You need to read up on basic Perl operations: I would recommend you get
    a good book.

    Ben

    --
    Raise your hand if you're invulnerable.
    []
     
    Ben Morrow, Feb 28, 2007
    #2
    1. Advertising

  3. Paul.Lee.1971

    Lambik Guest

    "Paul.Lee.1971" <> wrote in message
    news:...
    > Hi all,
    > I have a sequence of strings, for instance
    > "Hello, World "
    > "My First Perl Test "
    > "Exit(0) "
    >
    > - as you can see, the strings are all different lengths. Is there a
    > way to use rindex to find out the last non-whitespace character in the
    > string? I'd like to be able to use a general function to return
    > "Hello, World", "My First Perl Test" and "Exit(0).
    >

    this is a faq

    perldoc -q "blank space"

    after which you can check the length with:

    perldoc -f length
     
    Lambik, Mar 1, 2007
    #3
  4. Paul.Lee.1971

    -berlin.de Guest

    Ben Morrow <> wrote in comp.lang.perl.misc:
    >
    > Quoth "Paul.Lee.1971" <>:
    > > Hi all,
    > > I have a sequence of strings, for instance
    > > "Hello, World "
    > > "My First Perl Test "
    > > "Exit(0) "
    > >
    > > - as you can see, the strings are all different lengths. Is there a
    > > way to use rindex to find out the last non-whitespace character in the
    > > string?

    >
    > Not easily. For a task like this you want a regex. The index of the last
    > non-whitespace character in a string $str can be found with
    >
    > $str =~ /(\s*)$/ && length($str) - length($1) - 1;


    ....or

    $str =~ /\s*$/ && $-[ 0] - 1;

    Anno
     
    -berlin.de, Mar 3, 2007
    #4
  5. Paul.Lee.1971

    Dr.Ruud Guest

    Ben Morrow schreef:

    > $str =~ s/\s*$//;


    ITYM: $str =~ s/\s+$//;

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Mar 11, 2007
    #5
    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. Hadmut Danisch

    Array::index and rindex operator

    Hadmut Danisch, Jul 5, 2004, in forum: Ruby
    Replies:
    22
    Views:
    310
    David A. Black
    Jul 8, 2004
  2. STEPHEN BECKER I V

    rindex with array of arrays

    STEPHEN BECKER I V, Sep 28, 2004, in forum: Ruby
    Replies:
    3
    Views:
    200
    STEPHEN BECKER I V
    Sep 29, 2004
  3. trans.  (T. Onoma)

    rindex with regexp

    trans. (T. Onoma), Dec 28, 2004, in forum: Ruby
    Replies:
    2
    Views:
    132
    trans. (T. Onoma)
    Dec 28, 2004
  4. bwv549
    Replies:
    11
    Views:
    249
    Hugh Sasse
    Nov 7, 2008
  5. charliefortune

    find start and end of highlighted text in string

    charliefortune, Feb 15, 2007, in forum: Javascript
    Replies:
    1
    Views:
    92
    Une BĂ©vue
    Feb 15, 2007
Loading...

Share This Page