From 4fad56112022d60688e52fa75261785b51213831 Mon Sep 17 00:00:00 2001 From: John Denker Date: Fri, 20 Jul 2012 10:09:44 -0700 Subject: format stuff in columns --- tools/columns | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 tools/columns (limited to 'tools/columns') diff --git a/tools/columns b/tools/columns new file mode 100755 index 0000000..dcc934f --- /dev/null +++ b/tools/columns @@ -0,0 +1,33 @@ +#! /usr/bin/perl -w + +## not very scalable; stores entire document in memory + +use strict; +use List::Util qw[min max]; + +main:{ +## leftmost column is column ZERO + my @cwid = (); # width of column + + my @store = (); + + while (my $line = <>){ + chomp $line; + my @stuff = split(' ', $line); + for (my $ii = 0; $ii < 0+@stuff; $ii++){ + my $old = $cwid[$ii] || 0; + $cwid[$ii] = max(length($stuff[$ii]), $old); + } + push @store, \@stuff; + } + foreach my $line (@store) { + my @stuff = @$line; + for (my $ii = 0; $ii < 0+@stuff; $ii++){ + if ($ii) { + print " "; + } + printf("\%-$cwid[$ii]s", $stuff[$ii]); + } + print "\n"; + } +} \ No newline at end of file -- cgit v1.2.3