I am trying to figure out how to split a string based on length, while also keeping words together (word boundary). I have the split based on length part worked out, but can't figure out how to not split a word.

More info...
I am building a menu system. I have a subroutine for adding menu items that splits strings based on length, to maintain the width of the menu, but if I pass a string that contains a bunch of words, that exceed the menu's width, words get cut and makes the menu look very bad.

Example:

+------------------+ | This is my menu | +------------------+ | | +------------------+

The menuAdd() subroutine adds the pipes at the left and right side of the menu and either pads a short string with spaces or splits a long string into multiple lines.

My dilema:

menuAdd('A really long string that I want to add to the awesome menu s +ystem',20);
produces
+------------------+ | This is my menu | +------------------+ | | | A really long st | | ring that I want | | to add to the aw | | esome menu syste | | m | +------------------+
I want it to produce
+------------------+ | This is my menu | +------------------+ | | | A really long | | string that I | | want to add to | | the awesome menu | | system | +------------------+

This is what I have for the menuAdd() subroutine

sub menuAdd{ my $text = shift; my $menuWidth = shift; my $textLength = length($text); $menuWidth-=4; #shorten $menuWidth to leave room for | and a space + on left and right sides of the menu if ( $textLength <= $menuWidth ){ #add spaces to end of $text to maintain menuWidth print "| " . padSpaces($text,$menuWidth) . " |\n"; } elsif( $textLength > $menuWidth ){ #split $text into separate lines to maintain menuWidth my @textArray = split(/(.{$menuWidth})/, $text); print @textArray; foreach my $line (@textArray){ print "| " . padSpaces($line,$menuWidth) . " |\n"; #print "| $line |\n"; } } else { print "******SHOULD NEVER GET HERE******\n"; } }

In reply to How to split a string based on length or word boundary by bcarroll

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.