in reply to How to remove trailing white space when =~ s/\s+$//g; doesn't work

I think you getting confused with the trailing spaces and the whitespaces. Trailing spaces are the spaces that comes at the end of the last character. In your case "]" is the last character. Please look at the code below:

#!/usr/bin/perl use strict; use warnings; my $str1 = "The tailing spaces example 1 "; ## spaces after 1 $str1 =~ s/\s+$//g; my $str2 = "The tailing spaces example 2"; $str2 =~ s/\s+$//g; print "Output 1:",$str1,"\n"; ## this removes the spaces after 1 print "Output 2:",$str1,"\n"; ## does nothing print "Output 3:",$str1.$str2,"\n"; ## removes spaces and concatenates

OUTPUT:

Output 1:The tailing spaces example 1 Output 2:The tailing spaces example 2 Output 3:The tailing spaces example 1The tailing spaces example 2

Therefore, I would like to know what are you trying to do. If you want to remove the white spaces then you have to remove "$" from you substitution command i.e.:

$str1 =~ s/\s+//g;

the the output will be as follows:

Output 1:Thetailingspacesexample1 Output 2:Thetailingspacesexample2 Output 3:Thetailingspacesexample1Thetailingspacesexample2

Replies are listed 'Best First'.
Re^2: How to remove trailing white space when =~ s/\s+$//g; doesn't work
by Anonymous Monk on Oct 21, 2010 at 23:36 UTC
    Look more closely. The variable he's trying to remove a visible space from -- which apparently is only printing as a space but is some other invisible-at-the-terminal, non-space character -- is being interpolated into another string for printing. The string into which that variable is being interpolated is the one with the square brackets.