bohrme has asked for the wisdom of the Perl Monks concerning the following question:

I have a small script that acts like fortune in that it prints a random quote from a file of quotes. I currently use Text::Autoformat to (very nicely) format my paragraphs. My problem is that some of the quotes are quite long, eg, greater than 30 to 50 lines in length, and the text scrolls off the top of the screen.

Does anyone know of a way to split the text output into two columns if the length of the text output is greater than some arbitrary length? I.e., use one column if the length is less than a set number of lines and two if the length is greater than the pre-determined limit. I really like what Text::Autoformat does to my data so I would like to continue to use it to format my paragraphs.

Is this possible?

Thanks!

  • Comment on Two Column Output with Text::Autoformat

Replies are listed 'Best First'.
Re: Two Column Output with Text::Autoformat
by zwon (Abbot) on Oct 07, 2009 at 19:40 UTC

    Maybe something like this:

    use strict; use warnings; use Text::Autoformat; my $text = do { local $/; <>; }; my $formatted = autoformat $text, { left => 1, right => 38, justify => 'full', all => 1 }; my @lines = split /\n/, $formatted; if ( @lines > 50 ) { my $lines = int( ( @lines + 1 ) / 2 ); $formatted = ''; for ( 0 .. $lines - 1 ) { $formatted .= sprintf "%-38s %-38s\n", $lines[$_], ( $lines[ $_ + $lines ] ? $lines[ $_ + $lines ] : '' ); } } print $formatted;

      That is exactly what I was looking for. Thank you very much!