Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

How can I split a line on word boundaries closest to a certain length?

by Anonymous Monk
on Apr 21, 2000 at 19:18 UTC ( #8383=categorized question: print w/replies, xml ) Need Help??
Contributed by Anonymous Monk on Apr 21, 2000 at 19:18 UTC
Q&A  > strings


I've got a string of undertermined length, which will contain text with embedded newlines. I'd like to split this string into chunks of no more than 140 characters (but splitting on a word boundry, so as to not cut words apart). Ideally, I'd like to be left with an array containing the split strings.

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

and true to the perl motto, here's yet another (much simpler) way to do it:

@text = $messagebody =~ /(.{1,140}\W)/gms;
Answer: How can I split a line close to a certain length, but on a word boundary?
contributed by chromatic

The Text::Wrap module might prove useful to you. Your code might look like this:

use Text::Wrap qw(wrap 140 'wrap'); @strings = split/\n\n/, wrap($text);
Answer: How can I split a line close to a certain length, but on a word boundary?
contributed by poznick

I ended up using:

$rest = $messagebody;
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.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [Corion]: Heh - in university, I cheated on (a) by doing blackboard presentations using chalk. But those were 2 hour presentations, not quick/essential/ reduced presentations where you want to show something quick
    [ambrus]: (either on just one screen or two screens). this is necessary because
    [ambrus]: overhead slide plus blackboard is inconvenient because the lighting conditions are different and they require separate areas you can't quickly repartition, and typing on keyboard is faster and more convenient than writing on a blackboard
    [Corion]: (b) would be cool. I've thought about this doing Pod editing, and even simply regenerating/live updating the browser makes things much more interactive
    [ambrus]: modern computers have way enough processing power to allow this, at least for geeks who are willing to spend a few weeks to learn a tricky new user interface like vim
    [Corion]: ambrus: Well, for mathematical notation, I find blackboard much more convenient than a computer. But when inserting text or moving text around, the computer wins obviously
    [ambrus]: But either of these is a big problem in practice, so I'd need to spend like thirty years of my life to solve (a) and five more years to solve (b)
    [ambrus]: Corion: yes, CURRENTLY the blackboard is more convenient
    [ambrus]: and it's not like I want to ban blackboards anyway
    [ambrus]: you already have blackboards and a canvas for projector or overhead transparencies (or positive film slide projector, not used for maths) canvas in the same lecture halls today, and switch in a few minutes between presentations,

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (10)
    As of 2017-09-26 10:21 GMT
    Find Nodes?
      Voting Booth?
      During the recent solar eclipse, I:

      Results (293 votes). Check out past polls.