Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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?
Discipulus personae non gratae..
[Corion]: Your Mother: Hmm - I have the pmdev nodelet, which has some links, but I'm not sure what links/guides there are
[Corion]: pmdev wiki maybe?
[Discipulus]: uh neat karl!
[Your Mother]: Both good things to try, thank you!

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2017-07-21 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (319 votes). Check out past polls.