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

Answer: How can I split a line close to a certain length, but on a word boundary?

by poznick (Initiate)
on Apr 24, 2000 at 21:41 UTC ( #8730=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How can I split a line on word boundaries closest to a certain length? - Answer contributed by poznick

I ended up using:
$rest = $messagebody;
@text=();
while($rest ne '') {
    $rest =~ /(.{1,140}\W)/ms;
    push @text, $1;
    $rest = $';
}
Text::Wrap doesn't provide the desired behavior, unless I were to do a tr/// to translate all the newlines into some strange character, do the wrap, and then tr the strange characters back to newlines. This way seemed a little cleaner.

  • Comment on Answer: How can I split a line close to a certain length, but on a word boundary?
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2016-12-05 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (72 votes). Check out past polls.