#!/usr/bin/perl
# The Missing Textutils, Ondrej Bojar, obo@cuni.cz
# http://www.cuni.cz/~obo/textutils
#
# 'numerize_cols' replaces each column with a number, trying to preserve
# original number of characters.
# 
# This tool is useful for all those who are bad at counting columns. Use 'head
# -3 input | numerize_cols 1' to learn the number of the columns (starting from
# 1) quickly.
#
# $Id: numerize_cols,v 1.5 2012-01-23 08:36:16 bojar Exp $
#

use strict;
use Getopt::Long;
use POSIX;

my $indent = 0;
GetOptions(
  "indent" => \$indent,
) or exit 1;

my $base = shift;

my $nr = 0;
while (<>) {
  $nr++;
  my $origline=$_;
  my @line = split /\t/;
  chomp $line[1];

  if ($indent) {
    # indented output format
    my $lineno = $nr.":";
    my $voidlineno = (" " x length($lineno));
    my $colnum = scalar(@line) || 1;
    my $colnumwidth = ceil(log($colnum)/log(10));
    for(my $i=0; $i<@line; $i++) {
       printf "%s%${colnumwidth}i\t%s\n",
         ($i==0?$lineno:$voidlineno),
	 $i+$base,
	 $line[$i]
    }
    print "\n";
  } else {
    # default: preserve columns
    for(my $i=0; $i<@line; $i++) {
      my $num = $i+$base;
      my $nl = length($num);
      my $ll = length($line[$i]);
      if ($nl < $ll) {
        $num .= " ";
        $nl ++;
      }
      if ($ll < $nl) {
        # failing to produce less chars
        $line[$i] = $num;
      } else {
        $line[$i] = $num.substr($line[$i], $nl);
      }
    }
    print join("\t", @line)."\n";
    print $origline;
  }
}
