Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Removing extra spaces

by Athanasius (Abbot)
on Jul 31, 2012 at 02:09 UTC ( #984547=note: print w/ replies, xml ) Need Help??

in reply to Removing extra spaces

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";


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


Athanasius <°(((><contra mundum

Comment on Re: Removing extra spaces
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://984547]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2015-08-29 22:26 GMT
Find Nodes?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...

    Results (344 votes), past polls