#!/usr/bin/perl
# The Missing Textutils, Ondrej Bojar, obo@cuni.cz
# http://www.cuni.cz/~obo/textutils
#
# 'colidx' expects a string in ARG1. Then it reads the first line of its
# input/following arguments and scans the fields for the label.
# The column index containing the label is printed.
#
# $Id: colidx,v 1.1 2005/11/04 10:27:14 bojar Exp $
#

use strict;
use strict;
use Getopt::Long;
use List::Util 'first';

sub usage {
  print STDERR "colidx label <stdin >stdout
Returns the column index of the column labelled with label.
Options:
  --trim   ... remove spaces 
";
  exit 1;
}

my $usage = 0;
my $trim = 0;
GetOptions("help" => \$usage,
  "trim" => \$trim,
);
usage() if $usage;

my $label = shift;

$_ = <>;
chomp;
my @line = split /\t/;

@line = map { s/^ *| *$//g; $_ } @line if $trim;

my $idx = first { $line[$_] eq $label } (0..$#line);

if (!defined $idx) {
  print STDERR "Column '$label' not found";
  exit 1;
}
print ($idx+1)."\n";

