Hi monks,

Please suggest on how I can improve the code below that inserts appropriate breaklines into a long paragraph to achieve a desired vertical spacing between the lines.

use strict; my $string = q~Suppose you run a Web site that includes a banner adver +tisement service. You contract with companies that want their ads dis +played when poeple visit the pages on your site. Each time a visitor +hits one of your pages, you serve an ad embedded in the page that is +sent to the visitor's browser and assess the company a small fee. To +represent this information, you maintain three tables. One table, com +pany, has columns for company name, number, address, and telephone nu +mber. Another table, ad lists ad numbers, the number for the company +that owns the ad, and the amount you charge per hit. The third table, + hit, logs each ad hit by number and the date on which the ad was ser +ved.~; my @words = split / /, $string; my ($string_len, $new_string); foreach (@words) { my $space = 1; $string_len += length($_); if ($string_len >= 50) { $new_string .= "$_<br /><br />"; $string_len = 0; $space = 0; } else { $new_string .= $_; $new_string .= ' ' if $space; } } # output Suppose you run a Web site that includes a banner advertisement<br />< +br />service. You contract with companies that want their ads display +ed<br /><br />when poeple visit the pages on your site. Each time a v +isitor<br /><br />hits one of your pages, you serve an ad embedded in + the page that<br /><br />is sent to the visitor's browser and assess + the company a small<br /><br />fee. To represent this information, y +ou maintain three tables.<br /><br />One table, company, has columns +for company name, number, address,<br /><br />and telephone number. A +nother table, ad lists ad numbers, the<br /><br />number for the comp +any that owns the ad, and the amount you charge<br /><br />per hit. T +he third table, hit, logs each ad hit by number and the<br /><br />da +te on which the ad was served.
As usual, many thanks in advance :)

Update: Thanks all for your wonderful solutions :)


In reply to Insert breaklines into string by kiat

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.