Smart questions
Smart answers
Smart people
Join Tek-Tips Forums

Member Login

Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Why does globally replacing spaces give me the letter "s" instead?Helpful Member!(2) 

Zistrosk (Programmer) (OP)
6 Sep 11 23:35
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;


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,

Helpful Member!  prex1 (Programmer)
7 Sep 11 3:12
\s 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  \s  just represents the letter 's': you need to use the real space char there.
Also your regex is incorrect: try this one
s/\s+/ /g;
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
s/ +/ /g;

Franco : Online engineering calculations : Magnetic brakes for fun rides : Air bearing pads

Helpful Member!  Annihilannic (MIS)
7 Sep 11 3:28
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?

tgmlify - code syntax highlighting for your tek-tips posts

Zistrosk (Programmer) (OP)
7 Sep 11 6:06
Awesome, thanks!  That explains it :)

Appreciate your help.

1DMF (Programmer)
7 Sep 11 6:53
Have some stars guys!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close