in reply to extract values from file if greater than value

It sounds like the first two fields form a kind of ID. Can you count on the IDs in the two files being in the same order? Can the same ID appear more than once in either file?
  • Comment on Re: extract values from file if greater than value

Replies are listed 'Best First'.
Re^2: extract values from file if greater than value
by mulder4786 (Novice) on Jun 15, 2016 at 18:25 UTC
    Yes, they do form a unique ID, but no I cannot count on them to be in the same order. In fact the second file will be a subset of the first file in terms of ID
      So I see the requirements are becoming more refined. Ok, now...a re-write of my previous post...
      #!/usr/bin/perl use warnings; use strict; # this uses a "trick" to open an in memory file # like a file on the disk for testing purposes my $file1 =<<END; 1 19002930 0.74 1 19002931 -0.12 END my $file2 =<<END; 1 19002930 0.84 0.12 0.94 1 19002931 0 -.20 .12 END open (my $fh1, '<', \$file1) or die "$!"; open (my $fh2, '<', \$file2) or die "$!"; my %hash; #generate hash table from file 1 while (my $line=<$fh1>) { my ($Col1, $Col2, $Col3) = (split ' ', $line); $hash{"$Col1 $Col2"} = $Col3; } #if col1,2 from file 2 match, then output #col1,2 and all fields >= the col3 field from file1 while (my $line=<$fh2>) { my ($Col1, $Col2, @file2rest) = split ' ', $line; if (defined $hash{"$Col1 $Col2"}) { print "$Col1 $Col2 "; print join" ", grep{ $_>=$hash{"$Col1 $Col2"}}@file2rest; print "\n"; } } __END__ prints: 1 19002930 0.84 0.94 1 19002931 0 .12