Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Removing extra spaces

by rickoy (Initiate)
on Jul 31, 2012 at 00:59 UTC ( #984543=perlquestion: print w/ replies, xml ) Need Help??
rickoy has asked for the wisdom of the Perl Monks concerning the following question:

I have a string: 2 0 1 2 - 7 - 2 7 9 : 3 7 : 3 1 As you can see, each character in the string are separated by 1 space and the date and time are separated by 2 spaces. I would wish to reduce the spaces so that if there are 2 spaces in between characters, it will become 1 space and if there is 1 space in between characters, then it will be gone.

Comment on Removing extra spaces
Re: Removing extra spaces
by NetWallah (Abbot) on Jul 31, 2012 at 01:28 UTC
    Try this regex:
    s/\s\s?(\S)/$1/g
    Update: See the correction below. Thanks Anonymonk and davido.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Close, just drop the \s? and it works.

      $ perl -E '$s="2 0 1 2 - 7 - 2 7 9 : 3 7 : 3 1"; $s =~ s/\s\s?(\S)/$1 +/g; say $s' 2012-7-279:37:31 $ perl -E '$s="2 0 1 2 - 7 - 2 7 9 : 3 7 : 3 1"; $s =~ s/\s(\S)/$1/g; + say $s' 2012-7-27 9:37:31
Re: Removing extra spaces
by Rudolf (Monk) on Jul 31, 2012 at 01:56 UTC

    Being lazy, I would abuse the power of regex's and say:

    my $string = '2 0 1 2 - 7 - 2 7 9 : 3 7 : 3 1'; $string =~ s/ /x/g; $string =~ s/ //g; $string =~ s/x/ /g; print $string;

    just did it out in steps.. since you want to remove all the spaces I put a spot holder where all the double spaces are supposed to be, then later replaced the 'x' with ' '. perhaps give tr/// a look, that switches out sets but I'm not sure how to switch out spaces with it.

Re: Removing extra spaces
by Athanasius (Abbot) on Jul 31, 2012 at 02:09 UTC

    Update: rickoy, welcome to the Monastery!

    The specification is a little unclear, but assuming you want to (a) remove all single spaces, and (b) squash all sequences of 2 or more spaces down to a single space:

    #! perl use strict; use warnings; my $string = ' 2 0 1 2 - 7 - 2 7 9 : 3 7 : 3 1 '; # NB: 2 spaces here ^^ # (a) Remove single spaces 1 while $string =~ s/(^|[^ ])[ ]([^ ]|$)/$1$2/g; # (b) Squash multiple spaces down to one $string =~ s/[ ]{2,}/ /g; print "'", $string, "'\n";

    Outputs:

    '2012-7-27 9:37:31'

    HTH,

    Athanasius <°(((><contra mundum

Re: Removing extra spaces
by davido (Archbishop) on Jul 31, 2012 at 02:26 UTC

    s/\s(\s?)/$1/g

    Match a single space, and optionally a second space. Capture that second space if it exists. Replace with the capture, which will be either nothing, or the second space.


    Dave

Re: Removing extra spaces
by johngg (Abbot) on Jul 31, 2012 at 09:16 UTC

    You could use a negative look-ahead to replace any space that is not followed by a space with nothing. This will break down if there are more than two spaces though.

    knoppix@Microknoppix:~$ perl -E ' > $dateStr = q{ 2 0 1 2 - 7 - 2 7 9 : 3 7 : 3 1 }; > $dateStr =~ s{\s(?!\s)}{}g; > say $dateStr;' 2012-7-27 9:37:31 knoppix@Microknoppix:~$

    Cheers,

    JohnGG

Re: Removing extra spaces
by GrandFather (Sage) on Aug 02, 2012 at 01:47 UTC

    Where did your string come from? Strangeness of that sort looks like 16 bit Unicode strings or some such imported in some odd fashion into Perl where the high 0 byte (for an ASCII character) has been replaced by a space. Maybe you would be better to get the conversion right if possible rather than try to fix it up later?

    True laziness is hard work

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2015-05-25 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    In my home, the TV remote control is ...









    Results (480 votes), past polls