#!/usr/bin/perl -w

# ----------------------------------------------------------------
# John Kerl
# kerl.john.r@gmail.com
# 2003/08/18
#
# Performs a matrix transpose on ASCII text.  Use in conjunction with
# colprint for maximum readability.
#
# Sample input:
#
#   1 0 2
#   1 2 0
#   0 0 0
#   3 4 5
#
# After running through xpose | colprint:
#
#   1 1 0 3
#   0 2 0 4
#   2 0 0 5
# ----------------------------------------------------------------

@lines=<>;
$maxcols = 0;
$numrows = 0;

for ($i = 0; $i < @lines; $i++) {
	$line = $lines[$i];
 	chomp $line;
 	$line =~ s/^\s+//;
 	$line =~ s/\s+$//;
	next if $line =~ m/^$/;
 	my @fields = split /\s+/, $line;
 	$maxcols = @fields if @fields > $maxcols;
 	for ($j = 0; $j < @fields; $j++) {
 		$$matrix[$numrows][$j] = $fields[$j];
 	}
	$numrows++;
}

for ($j = 0; $j < $maxcols; $j++) {
	for ($i = 0; $i < $numrows; $i++) {
		print " " if ($i > 0);
		print $$matrix[$i][$j];
	}
	print "\n";
}
