Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does globally replacing spaces give me the letter "s" instead? 2

Status
Not open for further replies.

Zistrosk

Programmer
Aug 5, 2011
5
US
Hi everyone,

I'm running the latest Strawberry Perl, and needed to take a string with multiple spaces and compress them down so there would only be one space in place of many in a row.

Seemed simple to me, I'd just go change all occurrences of two consecutive spaces to a single space, in a while loop, until the condition failed. However, what I got back was very weird. Check this out:

#problem compressing double spaces globally

$ma = " this test pattern has extra spaces in it.";

print "before: --->" . $ma . "<--- \n";

while ($ma =~ m/\s{2,2}/) { $ma =~ s/\s{2,2}/\s/g;} # compress double spaces down to one each

print "space-compressed: --->" . $ma . "<--- \n";

exit 0;


output:

before: ---> this test pattern has extra spaces in it.<---

space-compressed: --->ss thisstests patternsshasss extrasssspacessss inssssit.<---


Why were some spaces compressed, and others turned in to the letter "s"?

The first pattern I tried yielded the same result:
while ($ma =~ m/\s\s/) { $ma =~ s/\s\s/\s/g;}


Any ideas?

Thank you very much,

Zistrosk
 
[tt]\s[/tt] is only for search patterns and stands for whitespace chars, that means space,tab,line feed, carriage return and possibly some more. In the replacement part [tt]\s[/tt] just represents the letter 's': you need to use the real space char there.
Also your regex is incorrect: try this one
[tt]s/\s+/ /g;[/tt]
and this works alone, you don't need to include it in a while loop.
Note also that the regex above will also replace any line feeds in your string. If you don't want this just do
[tt]s/ +/ /g;[/tt]

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
If you think about it... \s is a shortcut for multiple white space characters... tab, space and possibly others, in a similar way to \d being a shortcut for any digit. So how would perl know which one of those characters to use in your replacement string?

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Awesome, thanks! That explains it :)

Appreciate your help.

Zistrosk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top