in reply to A Little String Help Please

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

Replies are listed 'Best First'.
Re^2: A Little String Help Please
by GrandFather (Saint) on May 06, 2007 at 00:28 UTC

    The part generation code can be condensed a little through use of a regex:

    use strict; use warnings; my $n = shift; my $text = do {local $/; <DATA>}; $text =~ s/\n/ /g; my @lines = $text =~ /(.{1,$n})\s+/g; print "$_\n" for @lines; __DATA__

    which generates the same output given the same input as the sample above.


    DWIM is Perl's answer to Gödel
      ... can be condensed a little ...

      I'd say that was condensing it quite a lot :)

      A much simpler approach, I wish I'd thought of it.

      Cheers,

      JohnGG