anthonysj has asked for the wisdom of the Perl Monks concerning the following question: (data formatting)

I'm reading text from a file where each paragraph is stored as a single line. Some of the these lines are quite long. How can I print the text so that it wraps each paragraph to lines that are no more than 70 characters long?

Originally posted as a Categorized Question.

  • Comment on How to wrap text to a maximum width of 70 characters per line?

Replies are listed 'Best First'.
Re: How to wrap text to a maximum width of 70 characters per line?
by cwest (Friar) on Oct 18, 2000 at 19:39 UTC

    Text::Wrap does this.

    use Text::Wrap; $Text::Wrap::columns = 70; print Text::Wrap::fill( '', '', join '', <DATA> ); __DATA__ This is a huge paragraph that no doubt spans quite a number of columns +. My guess is that it may even span past 70 Columns, so we'll use the + nifty Text::Wrap module to limit the number of columns that this par +agraph can take up to 70.
Re: How to wrap text to a maximum width of 70 characters per line?
by japhy (Canon) on Oct 18, 2000 at 19:39 UTC

    You can use formats:

    format = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~ $string . $string = get_some_really_long_string(); write;

    See perldoc perlform.

Re: How to wrap text to a maximum width of 70 characters per line?
by cLive ;-) (Prior) on Apr 02, 2001 at 22:57 UTC

    (Assuming you don't want paragraphs to wrap in the middle of words.)

    Here's a nice little regex solution:

    $paragraph =~ s/([^\n]{0,70})(?:\b\s*|\n)/$1\n/gi;
    Or genericized to any width, determined by a variable:
    my $max_width = 40; $paragraph =~ s/([^\n]{0,$max_width})(?:\b\s*|\n)/$1\n/gio;

    Smart Substrings has some interesting insight. Combining that with the solution above:

    $paragraph =~ s/([^\n]{50,70})(?:\b\s*|\n)/$1\n/gi; while ( $paragraph =~ m/([^\n]{71})/ ) { $paragraph =~ s/([^\n]{70})([^\n])/$1\n$2/g; }

    In other words, split lines which are more than 70 chars long and include no spaces. (Not sure the loop is necessary...)

Re: How can I get perl to print a max. # of 70 characters per line from a file
by davorg (Chancellor) on Oct 18, 2000 at 19:41 UTC

    It's not exactly clear what you're trying to do, but here are a couple of possibilities:

    If you only want to show the first 70 characters of each line then read the lines in one at a line and use substr.

    If you want to reformat the paragraphs so that they are no more that 70 characters wide, then read the whole paragraph in and reformat it using Text::Wrap.

    Originally posted as a Categorized Answer.

Re: How can I get Perl to print a max. # of 70 characters per line from a file
by cLive ;-) (Prior) on Apr 03, 2001 at 12:07 UTC
    appendum: following this Smart Substrings node, let me just say that I'd combine my answer with that one, given the chance again*:
    $paragraph =~ s/([^\n]{50,70})(?:\b\s*|\n)/$1\n/gi; while ($paragraph =~ m/([^\n]{71})/) { $paragraph =~ s/([^\n]{70})([^\n])/$1\n$2/g; }
    ie, split lines that are more than 70 chars long and include no spaces. I think that covers all my bases, and I think the loop is needed, but am not sure - too tired to test, but you get the idea... :)

    cLive ;-)

    *When I was a kid, I was locked in my bedroom. I learnt English from an ABBA record. It was hard, but if I had to do the same again, I would my friend.

    Fernando

    - Milton Jones

    Originally posted as a Categorized Answer.