#!/usr/bin/perl
# The Missing Textutils, Ondrej Bojar, obo@cuni.cz
# http://www.cuni.cz/~obo/textutils
#
# 'colcheck' checks whether all the given files have the same number of
# tab-delimited columns.
#
# $Id: colcheck,v 1.3 2005/10/10 07:33:48 bojar Exp $

my $colcnt = undef;

my $didf = 0;
while ($f = shift) {
  $didf = 1;
  if (open FILE, $f) {
    $nr = 0;
    while (<FILE>) {
      $nr++;
      @line = split /\t/;
      my $got = scalar @line;
      $colcnt = $got if !defined $colcnt;
      if ($got != $colcnt) {
        print STDERR "$f:$nr:Expected $colcnt, but found $got columns.\n";
      }
    }
    close FILE;
  } else {
    print STDERR "$f:File not found\n";
  }
}

if (!$didf) {
  my $nr = 0;
  while (<>) {
    $nr++;
    @line = split /\t/;
    my $got = scalar @line;
    $colcnt = $got if !defined $colcnt;
    if ($got != $colcnt) {
      print STDERR "$nr:Expected $colcnt, but found $got columns.\n";
    }
  }
}

