Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by Plankton (Vicar)
on Oct 21, 2010 at 21:48 UTC ( [id://866649]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Wise Monks,

I have this code ...

sysread($rh,$buf,4096) || syslog ( "crit", "$prog : $!" ); if($buf) { my $client_hostname=unpack('a*', $buf); $client_hostname =~ s/\s+$//g; syslog( "crit", "client_hostname=[$client_hostname]" ); } else {
... where I read a string from a socket and write it out to the syslog but in my syslog I get this ...
Oct 21 14:33:26 karen serverd[2384]: client_hostname=[onetwothree ]
... the space did not get truncated. Why?

Replies are listed 'Best First'.
Re: How to remove trailing white space when =~ s/\s+$//g; doesn't work
by morgon (Priest) on Oct 21, 2010 at 22:45 UTC
    If  s/\s+$//g; does not remove trailing spaces then there are no trailing spaces (you probably have something that only LOOKS like spaces).
Re: How to remove trailing white space when =~ s/\s+$//g; doesn't work
by halfcountplus (Hermit) on Oct 21, 2010 at 22:11 UTC

    Very mysterious. Try and get a closer look at the string:

    print "$_ " foreach(unpack('C*',$client_hostname));

    I notice here that ascii 23 (octal 27), the "end of transmission block" character prints as a space, but is not removed by \s.

    ps. 'g' is superfluous in that regexp.

Re: How to remove trailing white space when =~ s/\s+$//g; doesn't work
by jwkrahn (Abbot) on Oct 22, 2010 at 01:30 UTC
    sysread($rh,$buf,4096) || syslog ( "crit", "$prog : $!" );

    You do realize that if sysread returns 0 it is not a critical error and $! will not be set.

    if($buf) {

    You do realize that the string "0" is also false?

    my $client_hostname=unpack('a*', $buf); $client_hostname =~ s/\s+$//g;

    If you change the unpack format from 'a*' to 'A*' you won't need the substitution on the next line and it will also remove "\0" characters.

      That's it! Thanks!!!
Re: How to remove trailing white space when =~ s/\s+$//g; doesn't work
by snape (Pilgrim) on Oct 21, 2010 at 23:13 UTC

    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
      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.
Re: How to remove trailing white space when =~ s/\s+$//g; doesn't work
by Anonymous Monk on Oct 22, 2010 at 02:55 UTC
    \p{Zs} or \p{Space_Separator}: a whitespace character that is invisible, but does take up space.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://866649]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-24 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found