Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: How to remove \n between two words but retain one space

by JediWizard (Deacon)
on Nov 27, 2005 at 04:01 UTC ( #511984=note: print w/replies, xml ) Need Help??


in reply to Re: How to remove \n between two words but retain one space
in thread How to remove \n between two words but retain one space

I believe that:

s/(?<=[[:alpha:]])\s+(?=[[:alpha:]])/ /g;

Will have the same effect as your posted expression. Although it will replace a single space character between two letters with a single space character (essentially a no-op), because it is a simpler pattern will actually take less time to execute. Below I am posting a simple benchmark I used to test this theory. If I have missed something please enlighten me.

#!/usr/local/bin/perl -w use strict; use Benchmark; my(@tests) = <DATA>; timethese(400, {his => sub{ s/(?<=[[:alpha:]])(?:\s\s+|[^\S ]+)(?=[[:a +lpha:]])/ /g foreach(@tests); }, mine => sub{s/(?<=[[:alpha:]])\s+(?=[[:alpha:]])/ /g f +oreach(@tests); }}); __DATA__ A sting with weird A string without weird Another variety with more wierd Anotherthingwithnospaces something odd soemthing normal [me@mylinux]$ ./space.pl Benchmark: timing 400 iterations of his, mine... his: 0 wallclock secs ( 0.07 usr + 0.00 sys = 0.07 CPU) @ 57 +14.29/s (n=400) (warning: too few iterations for a reliable count) mine: 0 wallclock secs ( 0.03 usr + 0.00 sys = 0.03 CPU) @ 13 +333.33/s (n=400) (warning: too few iterations for a reliable count)

They say that time changes things, but you actually have to change them yourself.

—Andy Warhol

Replies are listed 'Best First'.
Re^3: How to remove \n between two words but retain one space
by Your Mother (Archbishop) on Nov 27, 2005 at 06:19 UTC

    You're right. I was trying to be overly correct I think in avoiding replacing " " with " "; it slows down the works considerably so it's not the way to go for this.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://511984]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2021-06-18 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (87 votes). Check out past polls.

    Notices?