Re: Rindex used to find end of a string




Quoth "Paul.Lee.1971" <Paul.Lee.1971@xxxxxxxxxxxxxx>:
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@xxxxxxxxxxxx]
.



Relevant Pages

  • Re: Rindex used to find end of a string
    ... "My First Perl Test " ... the strings are all different lengths. ... perldoc -q "blank space" ...
    (comp.lang.perl.misc)
  • Re: Rindex used to find end of a string
    ... I have a sequence of strings, ... "My First Perl Test " ... the strings are all different lengths. ... way to use rindex to find out the last non-whitespace character in the ...
    (comp.lang.perl.misc)
  • Rindex used to find end of a string
    ... I have a sequence of strings, ... "My First Perl Test " ... the strings are all different lengths. ...
    (comp.lang.perl.misc)
  • Re: Cantorian pseudomathematics
    ... >> MoeBlee said: ... >> strings of finite length. ... The set of all binary strings of all finite lengths is a countable ... Right, and all of your naturals are finite, so this is true for all of them. ...
    (sci.math)
  • Re: [Lit.] Buffer overruns
    ... > headers with max/current lengths and strings have headers with current ... > in the majority of buffer overrun/overflow problems. ... as character arrays and for passing and working constant character arrays ...
    (sci.crypt)