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

OK, I know this is probably simple but for some reason it's not coming to me right now. Suppose I have a string that contains spaces and also has trailing spaces, how do I get rid of the trailing spaces without getting rid of the spaces between?
my $string = "Last Known "; $string =~ tr/ //s;
This will give me "Last Known ", but what I need is "Last Known". I appreciate any wisdom that may be imparted.

Replies are listed 'Best First'.
Re: Trailing spaces
by thraxil (Prior) on Jun 22, 2001 at 01:03 UTC
Re: Trailing spaces
by VSarkiss (Monsignor) on Jun 22, 2001 at 01:04 UTC
    You're very close: $string =~ s/\s*$//; As far as I know, tr can't tell position. One space is the same as any other. Hence the s///.

    HTH

    Update: Oops. Forgot the quantifier.

    YA Update: Abigail is correct, + is better. That's what happens when you rush to fix an "oops"....

      Using s/\s*$// is not to be recommended, as that will always match (and hence do a substitution). Use of * means zero or more times. Use + instead.

      -- Abigail

Re: Trailing spaces
by Beatnik (Parson) on Jun 22, 2001 at 13:36 UTC
Re: Trailing spaces
by IcyHot (Novice) on Jun 23, 2001 at 01:08 UTC

    It's best to use the end-of-string or end-of-line metacharacters which solves the problem of not getting rid of the spaces between "Last" and "Known".

    The dollar-sign $ character means end of line or end of string if the string has only one line.
    The \s means a space so you don't need a physical space " "

    It's also probably better to use s/// (substitution) rather than tr/// (tranliteration) although either should work.

    s/\s*$//; # means substitute zero or more spaces # before end-of-line with NOTHING (get rid of them)
    Here's some basic sample code:
    $jo = "Mary had a little lamb "; print $jo . "blah\n"; $jo =~ s/\s*$//; # get rid of the spaces print $jo . "blah\n";
    Here's the output:
    Mary had a little lamb blah # spaces at end of $jo Mary had a little lambblah # spaces be gone!
    ---------------------------------------------------------
    There's also the \z character which means end of string, no matter what (ragardless of any newlines).
    \Z matches anything right before a newline at the end of a string or the end of a string if there is no newline.
    The $ character is pretty much the same as \Z unless the string has several newlines (string is a paragraph) and then the $ would match before a newline character, not necessarily the end of the string.

    Therefore, if you have a paragraph and want to get rid of spaces at the end of EVERY line, you could do something like:

    s/\s*$//gm;

    The /g applies the substitution to multiple matches, while /m tells Perl that there are Multiple lines (as opposed to /s Single line)
    \A and ^ are the same as \Z and $ but in the begging of a line or a string - hey, A is the first and Z is the last letter of the alphabet :)

    If you're not too familiar with regular expressions, you should read up on the different metacharacters you can you (don't lose your marbles, though - it can get hectic)

Re: Trailing spaces
by Anonymous Monk on Jun 22, 2001 at 13:12 UTC

    $string =~ s/ +$//;

    Allen

Re: Trailing spaces
by princepawn (Parson) on Jun 22, 2001 at 01:07 UTC
    I recommend String::Strip instead of all that comic-book profanity above {grin}.
    @foreign = qw(HTTP HTML CGI SQL fixed-length-data xSV-data); @chromatic = @tilly = map { "$_ is plain text, easily manipulated in Perl" } @foreign; @princepawn = map { "find something on CPAN to manipulate $_" } @forei +gn;
Re: Trailing spaces
by prarie dawn (Sexton) on Jun 22, 2001 at 06:33 UTC
    What if there are two non-trailing spaces together that need preserving?
      $string =~ s/\s+$//;

      will work fine for that, the $ anchors the pattern to the end of the string. Any other spaces that aren't at the end of the string are left as is.