Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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?
[davido]: I have nvidia (1050ti) in one system at home. My work laptop came with a radeon/amd video system that is no longer supported by debian/ubuntu. So that being the case I'm in the "prefer nvidia even if proprietary" camp
[davido]: Especially since the laptop is only a couple years old.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2017-05-26 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?