I think you are asking to wrap text to a given length, not a given number of words. The script here takes the wrap length as a command-line argument but it uses a hard coded data file at the moment; that would be easy to change. Given this data file

Now is the winter of our discontent Made glorious summer by this sun of York; And all the clouds that lour'd upon our house In the deep bosom of the ocean buried. Now are our brows bound with victorious wreaths; Our bruised arms hung up for monuments; Our stern alarums chang'd to merry meetings, Our dreadful marches to delightful measures. Grim-visag'd war hath smooth'd his wrinkled front; And now, -- instead of mounting barbed steeds To fright the souls of fearful adversaries, -- He capers nimbly in a lady's chamber To the lascivious pleasing of a lute. But I, -- that am not shap'd for sportive tricks, Nor made to court an amorous looking-glass; I, that am rudely stamp'd, and want love's majesty To strut before a wanton ambling nymph; I, that am curtail'd of this fair proportion, Cheated of feature by dissembling nature, Deform'd, unfinish'd, sent before my time Into this breathing world scarce half made up, And that so lamely and unfashionable That dogs bark at me as I halt by them; -- Why, I, in this weak piping time of peace, Have no delight to pass away the time, Unless to spy my shadow in the sun, And descant on mine own deformity: And therefore, -- since I cannot prove a lover, To entertain these fair well-spoken days, -- I am determined to prove a villain, And hate the idle pleasures of these days. Plots have I laid, inductions dangerous, By drunken prophecies, libels, and dreams, To set my brother Clarence and the king In deadly hate the one against the other: And if King Edward be as true and just As I am subtle, false, and treacherous, This day should Clarence closely be mew'd up, -- About a prophecy which says that G Of Edward's heirs the murderer shall be. Dive, thoughts, down to my soul: -- here Clarence comes.

the script

use strict; use warnings; my $partLen = shift or die qq{No part length supplied\n}; die qq{Part length not integer\n} unless $partLen =~ m{^\d+$}; my $string = q{}; my $wordsFile = q{winter.txt}; open my $wordsFH, q{<}, $wordsFile or die qq{open: $wordsFile: $!\n}; { local $/; $string = <$wordsFH>; } close $wordsFH or die qq{close: $wordsFile: $!\n}; my @words = split m{\s+}, $string; my $longestWord = ( sort { $b <=> $a } map { length } @words )[0]; die qq{Part length too small to accomodate longest word\n} if $partLen < $longestWord; my @parts = (); my $part = q{}; while ( my $word = shift @words ) { $part = $word, next unless $part; if ( length($part) + length($word) + 1 > $partLen ) { push @parts, $part; $part = $word; } else { $part .= qq{ $word}; } } push @parts, $part; print qq{$_\n} for @parts;

given an argument of 33, produces

Now is the winter of our discontent Made glorious summer by this sun of York; And all the clouds that lour'd upon our house In the deep bosom of the ocean buried. Now are our brows bound with victorious wreaths; Our bruised arms hung up for monuments; Our stern alarums chang'd to merry meetings, Our dreadful marches to delightful measures. Grim-visag'd war hath smooth'd his wrinkled front; And now, -- instead of mounting barbed steeds To fright the souls of fearful adversaries, -- He capers nimbly in a lady's chamber To the lascivious pleasing of a lute. But I, -- that am not shap'd for sportive tricks, Nor made to court an amorous looking-glass; I, that am rudely stamp'd, and want love's majesty To strut before a wanton ambling nymph; I, that am curtail'd of this fair proportion, Cheated of feature by dissembling nature, Deform'd, unfinish'd, sent before my time Into this breathing world scarce half made up, And that so lamely and unfashionable That dogs bark at me as I halt by them; -- Why, I, in this weak piping time of peace, Have no delight to pass away the time, Unless to spy my shadow in the sun, And descant on mine own deformity: And therefore, -- since I cannot prove a lover, To entertain these fair well-spoken days, -- I am determined to prove a villain, And hate the idle pleasures of these days. Plots have I laid, inductions dangerous, By drunken prophecies, libels, and dreams, To set my brother Clarence and the king In deadly hate the one against the other: And if King Edward be as true and just As I am subtle, false, and treacherous, This day should Clarence closely be mew'd up, -- About a prophecy which says that G Of Edward's heirs the murderer shall be. Dive, thoughts, down to my soul: -- here Clarence comes.

I hope this is of use.

Cheers,

JohnGG


In reply to Re: A Little String Help Please by johngg
in thread A Little String Help Please by craigt

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.