Not knowing if the fields may repeat in either file as anonymous monk asked, I can only guess a solution. The best way to solve this would probably use a lookup hash (as in my solution below), provided the first 2 fields in file 1 don't repeat.
#!/usr/bin/perl use strict; use warnings; open my $fh1, '<', \<<EOF or die $!; 1 19002930 0.74 1 19002931 -0.12 EOF my %data; while (<$fh1>) { next if /CHR/; my ($chr, $bp, $min) = split; $data{$chr,$bp} = $min; } open my $fh2, '<', \<<EOF or die $!; 1 19002930 0.84 0.12 0.94 1 19002931 0 -.20 .12 EOF while (<$fh2>) { my ($chr, $bp, @rest) = split; if (defined(my $min = $data{$chr,$bp})) { print join(" ", $chr, $bp, grep $_ >= $min, @rest), "\n"; } }
This only prints the values - not sure how you want to store in an array (as you mentioned).

Update: Added the 'defined' operator to the if statement so a 'min' value of '0' will be accepted. Without testing for defined, a '0' value would cause the if statement to be wrongly false.

Also, like Marshall's solution, the temporary files I created were just for this example. You would need to open your files in the normal way open my $fh1, '<', 'yourfilename' or die $!.

Update 2: Noting Marshall's comment on separating $data{"$chr$bp"} by a space to be safer, $data{"$chr $bp"}, I used the seldom used idiom $data{$chr,$bp} where a comma separated series of terms as the key to a hash are joined together by the '$SUBSCRIPT_SEPARATOR', $;.

Also, I'm wondering what the purpose of next if /CHR/; is in his code. It is hard to see whithout a better data sample for the file he is reading.


In reply to Re: extract values from file if greater than value by Cristoforo
in thread extract values from file if greater than value by mulder4786

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.