Gossamer Forum
Home : General : Perl Programming :

Wrapping a line of text?

Quote Reply
Wrapping a line of text?
Hi,

Can anyone tell me how to do the following...

I have a variable $msg that contains the following string:

Hello how are you this is my messge to you for you to read. I've made the message quite long to demonstrate what I want to achieve.

What I want to do is wrap it after every space that follows the 20th character - so the result from the example above would be...


Hello how are you this
is my messge to you
for you to read. I've
made the message quite
long to demonstrate
what I want to achieve.


Cheers,
Regan.


Quote Reply
Re: Wrapping a line of text? In reply to
$msg =~ s/(.{20}\S*)\s/$1\n/g;


- Mark

Astro-Boy!!
http://www.zip.com.au/~astroboy/
Quote Reply
Re: Wrapping a line of text? In reply to
LOL!

That worked like a charm - thanks mark!

Check out my disaster attempt!

$Message =~ s/^[+{34,}]\S$/\n/g;

LOL!

Regan.


Quote Reply
Re: Wrapping a line of text? In reply to
One more question for you...

What would the code be if I wanted to find the 20th character, then go BACK and wrap the text at the "previous" space?

Thanks again,
Regan.