dkhalfe has asked for the wisdom of the Perl Monks concerning the following question:
hi all
Gameplan: I am going to read in a filter_file and an input file.
Filter file will be tab-delim formatted as such:
column relationship value num_or_string filter_or_append order a <= 0.3 num filter 1 b eq abc string append 3 c <= 0.3 num filter 2
Input file will be tab-delim formatted as such:
a b c 0.2 abc 0.3 0.1 abd 0.3 0.4 abe 0.2 0.1 abc 0.5 0.7 abt 0.7 0.1 abd 0.8
My code:
#!/usr/bin/perl use warnings; use strict; use autodie; use Data::Dumper; @ARGV == 2 or die "Invalid number of arguments. Please re-run program +and suppy as arguments \n1) the filter file and \n2) the input file." +; my ($filter_file, $input_file) = @ARGV; open (my $FILTER,"<","$filter_file") or die "Cannot open filter file: +$!"; my @filter_array; <$FILTER>; while (my $line = <$FILTER>) { chomp $line; my @line_array = split(/\t/, $line); push (@filter_array, \@line_array); } @filter_array = sort { $a->[5] <=> $b->[5]} @filter_array; #print Dumper \@filter_array, "\n"; #[DEBUGGING] my $num_elements = (@filter_array-1); #print "$num_elements", "\n"; #[DEBUGGING] open (IN,"<","$input_file") or die "Cannot open input file: $!"; open (OUTFILE, ">>", "OUTPUT_$input_file") or die "Cannot create an ou +tput file: $!"; for (my $i=0; $i<=$num_elements; $i++) { my $search_string = $filter_array[$i][0]; # print "$search_string", "\n"; my $header = <IN>; my @header_titles = split /\t/, $header; my $extract_col = 0; for my $header_line (@header_titles) { last if $header_line =~ m/$search_string/; $extract_col++; } print "Extracting column $extract_col\n"; while (my $row = <IN>) { last unless $row =~ /\S/; chomp $row; my @cells = split /\t/, $row; # print "$cells[$extract_col]\n"; if ((eval "$cells[$extract_col] $filter_array[$i][1] $filt +er_array[$i][2]")) { print OUTFILE "$cells[$extract_col]", "\n"; } } }
The part I need help with concerns the bottom of my code:
if ((eval "$cells[$extract_col] $filter_array[$i][1] $filter_array[$i] +[2]")) { print OUTFILE "$cells[$extract_col]", "\n"; }
NOTE: above code translates to: if values in col 'a' are '<=' to '.3', print value (those that match the criteria).
I need to match the column header of my input file with one designated in the filter file. Which I do here: last if $header_line =~ m/$search_string/; Then I need to make the comparison designated in the filter file. Ex: Match 'a' (column header) with 'a' in input file, if values in col 'a' are <= .3, print to OUTFILE. Which is done here:
My problem: If the values in col 'a' are <= .03, I need to print the entire row of the input file to the output file, not just the value from the column I am working with. Any help with this issue would be much appreciated. Thanks.if ((eval "$cells[$extract_col] $filter_array[$i][1] $filter_array[$i +] +[2]")) { print OUTFILE "$cells[$extract_col]", "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: match search string with column header, do some filtering with matched column, print lines/rows that match filter criteria
by Corion (Patriarch) on Jul 26, 2012 at 20:54 UTC | |
|
Re: match search string with column header, do some filtering with matched column, print lines/rows that match filter criteria
by Anonymous Monk on Jul 27, 2012 at 07:25 UTC | |
|
Re: match search string with column header, do some filtering with matched column, print lines/rows that match filter criteria
by CountZero (Bishop) on Jul 27, 2012 at 19:45 UTC | |
|
Re: match search string with column header, do some filtering with matched column, print lines/rows that match filter criteria
by CountZero (Bishop) on Jul 27, 2012 at 20:46 UTC |