Mr. Shawn H. Corey wrote:
On Tue, 2008-11-04 at 16:30 +0100, Rob Coops wrote:
What you want to be doing is this: $temp =~ s/\s+/ /g;
Actually to substitute multiple spaces with just one:
$temp =~ s/ +/ /g;
Or as some prefer:
$temp =~ s{ [ ]+ }{ }gx;
Not if speed is an issue:
$ perl -le
Sharan Basappa wrote:
Hi,
Hello,
I have string that has one or more spaces. I would like to replace
them with a single space.
$ perl -le'
my $temp = "0 1 2 34";
print $temp;
$temp =~ tr/ //s;
print $temp;
'
0 1 2 34
0 1 2 3 4
The simple code below replaces the spaces fine,
On Tue, 2008-11-04 at 16:37 +0100, Rob Coops wrote:
> On Tue, Nov 4, 2008 at 4:35 PM, Sharan Basappa <[EMAIL PROTECTED]>wrote:
> > Thanks. BTW, a variable ($x = " ") instead of actual space would do, right?
> >
>
> Yeah that should work just fine.
It is better to call it $SPACE:
my $SPACE = " ";
On Tue, 2008-11-04 at 16:30 +0100, Rob Coops wrote:
> What you want to be doing is this: $temp =~ s/\s+/ /g;
Actually to substitute multiple spaces with just one:
$temp =~ s/ +/ /g;
Or as some prefer:
$temp =~ s{ [ ]+ }{ }gx;
Or even:
$temp =~ s/\x20+/\x20/g;
The reason for not using
On Tue, Nov 4, 2008 at 4:35 PM, Sharan Basappa <[EMAIL PROTECTED]>wrote:
> > You are completely right. :-)
> >
> > What you want to be doing is this: $temp =~ s/\s+/ /g;
> > The reason for that is simple, \s is used to match a space or multiple
> > spaces, it is not used to print a space that is a
> You are completely right. :-)
>
> What you want to be doing is this: $temp =~ s/\s+/ /g;
> The reason for that is simple, \s is used to match a space or multiple
> spaces, it is not used to print a space that is actually done by the ' '
> (space). It might seem a little strange at first but just
On Tue, Nov 4, 2008 at 4:21 PM, Sharan Basappa <[EMAIL PROTECTED]>wrote:
> Hi,
>
> I have string that has one or more spaces. I would like to replace
> them with a single space.
> The simple code below replaces the spaces fine, but does not
> substitute with a space.
>
> $temp = "0 1 2 34";
Hi,
I have string that has one or more spaces. I would like to replace
them with a single space.
The simple code below replaces the spaces fine, but does not
substitute with a space.
$temp = "0 1 2 34"; <-> version 1
$temp =~ s/\s+/\s/g;
$temp = "0 1 2 34"; <-> version 2
$temp =~ s