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:

if ((eval "$cells[$extract_col] $filter_array[$i][1] $filter_array[$i +] +[2]")) { print OUTFILE "$cells[$extract_col]", "\n"; }
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.


In reply to match search string with column header, do some filtering with matched column, print lines/rows that match filter criteria by dkhalfe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.