#!/usr/bin/perl

# format tab separated table data, pad between colums with $pad spaces
# strip leading/trailing whitespace from each data item
# right align all items, justify/pad lines

my $pad=3;
my @lengths=();
my @data=();
while (<>){
	chomp;
	{
		my $row=[];
		@{$row}=split(/\t/);
		for my $n (0..$#{$row}){
			$_=$row->[$n];
			s/^ +//o; # strip leading spaces
			s/ +$//o; # strip trailing spaces
			$row->[$n]=$_;
			if(length($row->[$n])>$lengths[$n]){
				$lengths[$n]=length($row->[$n]);
			};
		};
		push (@data,$row);
	}
};
for my $row (@data){
	for my $column (0..$#lengths){
		$_=$row->[$column];
		my $format=$lengths[$column];
		$format+=$pad if $column;
		$format="%${format}s"; # for printf
		if($column<$#lengths){
			printf($format,$_);
		}else{
			printf("$format\n",$_);
		};
	};
};
