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