#!/usr/bin/perl
# The Missing Textutils, Ondrej Bojar, obo@cuni.cz
# http://www.cuni.cz/~obo/textutils
#
# 'coltest' is a generalization of colgrep. An arbitrary perl code is evaluated
# at each line. The code cat refer to any of the columns using $1, $2, etc.
#
# $Id: coltest,v 1.4 2009-06-16 06:21:27 bojar Exp $
#

use strict;
use Getopt::Long;

my $inverse = 0;
my $verbose = 0;
my $skip = 0;
my $utf8 = 0;
GetOptions(
  "skip=i" => \$skip,
  "inverse" => \$inverse,
  "verbose"=>\$verbose,
  "utf8"=>\$utf8,
) or exit 1;

my $test=shift;

if (!defined $test) {
  print STDERR "usage: coltest \"\$1 < 0.5\"
Options:
  --inverse  ... negate the test
  --skip=N ... copy the first N lines unconditionally
  --verbose  ... print the perl code for test
  --utf8  ... assume input and output in UTF-8
";
  exit 1;
}

if ($utf8) {
  binmode(STDIN, ":utf8");
  binmode(STDOUT, ":utf8");
  binmode(STDERR, ":utf8");
}

sub fix {
  my $i = shift;
  $i--;
  return "\$line[$i]";
}

$test =~ s/\$([0-9]+)/fix($1)/ge;

print "TEST: $test\n" if $verbose;

while ($skip > 0) {
  $_ = <>;
  print;
  $skip --;
}

while (<>) {
  my $origline = $_;
  chomp;
  my @line = split /\t/;
  my $hit = eval($test);
  print $origline if ($hit && !$inverse) || (!$hit && $inverse);
}

