#!/usr/bin/perl
use Getopt::Long;
$opt_c=-1;
$opt_d=-1;
$opt_z=-1;
GetOptions(qw(c:i d:i z:i s h line:s cond:s));
$0=~s/.*\///; 
if ( $opt_h ) {
print "

extract columns of data from different text 
files and put them column wise together

use $0 like gnuplot e.g. 
  $0 file1 i 0 u 1:2 file2 i 2 u 3 
  takes column 1 and 2 from dataset 0 in file1 and column 3 from dataset 2 in file2

  datasets are separated by 2 blank lines 

  -cond \'condition\'   e.g.   \$3>0   (column 3 greater than zero) 
  -c nr                       cumulate (sum) from column nr
  -d nr                       sum up column nr*column one 

\n";
exit;
}
$nfile=0;
$indexMin=-1;
$indexMax=-1;
@using=(1,2);
$usingMax=2;
while (@ARGV) {
    open(IN,"<$ARGV[0]"); shift;
    if( $ARGV[0] =~ /^i$/ ) {
	shift;
	$indexMin=$ARGV[0];
	$indexMax=$indexMin;
	shift;
	if ( $indexMin =~ /(.*)-(.*)/ ) { 
	    $indexMin=$1;
	    $indexMax=$2;
	} 
#	print "range: $indexMin - $indexMax \n";
    }
    if( $ARGV[0] =~ /^u$/ ) {
	shift;
	@using=split(':',$ARGV[0]);
	shift;
	$usingMax=0;
	foreach $i ( @using ) {$usingMax=$i if $i>$usingMax}
#	print "using: @using max== $#using Max=$usingMax\n";
    }
    $flag=0;
    $curIndex=0;
    $nindex=0;
    while (<IN>) {
	next if /^\s*#/ ;                   # skip comment lines 
	if ( /^\s*$/ ) {
	    $flag++;
	    # print "flag:$flag\n";
	    if ( $flag==2  ) {
		$curIndex++;
		# print "curIndex:$curIndex\n";
	    }
	    next;
	} else {
	    $flag=0;
	}
	(@values)=split;
	if($#values < $usingMax-1 ) { next; }
	if( ( $curIndex >= $indexMin && $curIndex <= $indexMax ) || $indexMax == -1 ) {
#	    print "val[$nfile][$nindex]: $values[$using[$i]-1]\n";
	    (@a)=split;
	    for ( $i=0; $i<=$#using;$i++) {
		if ($using[$i] =~ /^\(/ ) {
		    ($u=$using[$i]) =~ s/\$([0-9]+)/\$a\[-1+\1\]/g;
		    $val[$nfile+$i][$nindex]=eval($u); 
		} else {
		    $val[$nfile+$i][$nindex]=$values[$using[$i]-1];
		}
	    }
	    if ( defined($opt_cond)) {
		($u=$opt_cond) =~ s/\$([0-9]+)/\$a\[-1+\1\]/g;
		# print "here",eval($u),"--->$u<---$opt_cond\n";
		if( ! eval($u) ) {$nindex--;}
	    }
	$nindex++;
	}
    }
    $nfile+= $#using+1; 
    $Maxnindex=$nindex > $Maxnindex ? $nindex : $Maxnindex;
}

if ( $opt_s ) {
    for ( $i1=0;$i1<$Maxnindex-1;$i1++) {
	for ( $i2=$i1+1;$i2<$Maxnindex;$i2++) {
	    if ( $val[0][$i1] > $val[0][$i2] ) {
		for ($j=0;$j<$nfile;$j++) {
		    $c=$val[$j][$i1];
		    $val[$j][$i1]=$val[$j][$i2];
		    $val[$j][$i2]=$c;
		}  
	    } 
	}
    }
    if( 1 ) {
    $nf=0;
    for ( $i1=1;$i1<$Maxnindex;$i1++) {
	if( $val[0][$i1] != $val[0][$i1-1] ) {
	    $nf++;
	    for ($j=0;$j<$nfile;$j++){$val[$j][$nf]=$val[$j][$i1]};
	} else {
	    for ($j=1;$j<$nfile;$j++){$val[$j][$nf]+=$val[$j][$i1]};
	}
    }
    $nf++;
    $Maxnindex=$nf;
}
}

for ( $i=0;$i<$Maxnindex;$i++) {
    if ( ( $opt_z > -1 &&  ($val[$opt_z][$i]) > 0  )  || $opt_z < 0 ) {
	for ($j=0;$j<$nfile;$j++) {
	    if( $opt_d == $j ) { $val[$j][$i]=$val[$j][$i-1]
				     +$val[$j][$i]*$val[0][$i];}
	    if( $opt_c == $j && $i >= 1 ) { $val[$j][$i]+=$val[$j][$i-1];}
	    print "",$val[$j][$i],"\t";
	}
	print "\n";
    }
}
