summaryrefslogtreecommitdiff
path: root/tools/columns
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-07-20 10:09:44 -0700
committerJohn Denker <jsd@av8n.com>2012-07-29 15:32:34 -0700
commit4fad56112022d60688e52fa75261785b51213831 (patch)
tree539839c462dcf498d541bc6f5c10fb0cda6d77e1 /tools/columns
parentabcd53ddce872e3f331d8a6d7c1ff44c070b91b0 (diff)
format stuff in columns
Diffstat (limited to 'tools/columns')
-rwxr-xr-xtools/columns33
1 files changed, 33 insertions, 0 deletions
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