All pastes #900666 Raw Edit

columnar

public text v1 · immutable
#900666 ·published 2008-02-12 02:05 UTC
rendered paste body
#!/usr/bin/perl
use strict;

my $width = 80;
my $len = 0;
my $columns = 5;
my $column_stop = int($width/$columns);

while(<>){
        my $line_length = 0;
        my @words = split(/ +/, $_);
        foreach my $word (@words){
                my $col = 0;
                for(my $i=1;$i<=$columns;$i++){
                        if($i*$column_stop > $line_length + length($word)){
                                $col = $i;
                        }
                }
                if($col == 0){
                        print "\n";
                        $line_length = 0;
                }

                while(length($word) < $column_stop){
                        $word .= " ";
                }
                $line_length += length($word);
                print $word;
        }
}