Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to split a string based on length or word boundary

by BrowserUk (Pope)
on Dec 21, 2012 at 02:40 UTC ( #1009835=note: print w/ replies, xml ) Need Help??


in reply to How to split a string based on length or word boundary

As long as your text doesn't contain any single words longer than your width:

$s = 'A really long string that I want to add to the awesome menu sys +tem';; print "| $_ |" for map substr( $_ . ' 'x16, 0, 16 ), $s =~ m[(.{1,16}) +\s+]g;; | A really long | | string that I | | want to add to | | the awesome menu |

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong


Comment on Re: How to split a string based on length or word boundary
Download Code
Re^2: How to split a string based on length or word boundary
by Athanasius (Monsignor) on Dec 21, 2012 at 02:54 UTC

    Brilliant++!

    Just needs a couple of tweaks:

    print "| $_ |\n" for map substr($_ . ' ' x 16, 0, 16), $s =~ m[(.{1,16 +})(?:\s+|$)]g; # ^^ + ^^^^^^^^^

    (Otherwise, the final word won’t be printed unless it happens to be followed by whitespace.)

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Good point about the $++

      The \n is provided by -l in my code.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

Re^2: How to split a string based on length or word boundary
by bcarroll (Pilgrim) on Dec 21, 2012 at 03:08 UTC
    That works! Thanks for the quick reply. I changed the elsif code to
    print "| $_ |\n" for map substr( $_ . ' 'x$menuWidth, 0, $menuWidth ), + $text =~ m[(.{1,$menuWidth})\s+]g;

      As Athanasius pointed out above, you need to change that to:

      print "| $_ |\n" for map substr( $_ . ' 'x$menuWidth, 0, $menuWidth ), + $text =~ m[(.{1,$menuWidth})(?:\s+|$)]g; #..................................................................... +.............................^^^^^^^^^

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009835]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2014-12-21 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls