Re: substitute multiple spaces with just one

2008-11-04 Thread John W. Krahn
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

Re: substitute multiple spaces with just one

2008-11-04 Thread John W. Krahn
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,

Re: substitute multiple spaces with just one

2008-11-04 Thread Mr. Shawn H. Corey
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 = " ";

Re: substitute multiple spaces with just one

2008-11-04 Thread Mr. Shawn H. Corey
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

Re: substitute multiple spaces with just one

2008-11-04 Thread Rob Coops
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

Re: substitute multiple spaces with just one

2008-11-04 Thread Sharan Basappa
> 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

Re: substitute multiple spaces with just one

2008-11-04 Thread Rob Coops
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";

substitute multiple spaces with just one

2008-11-04 Thread Sharan Basappa
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