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 3 4"; <-> version 1 > $temp =~ s/\s+/\s/g; > > $temp = "0 1 2 3 4"; <-> version 2 > $temp =~ s/\s+/s/g; > > They both end up actually substituting spaces with string s > > Regards > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > http://learn.perl.org/ > > > 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 try and think of how you would read something like s/ {10}/........../g; that would make no sense at all and you would have to go count the number of spaces so when matchin you use \s to represend a space, but when printing or substituting you simple use the ' ' (space).