#!/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
In reply to Re^3: extract values from file if greater than value
by Marshall
in thread extract values from file if greater than value
by mulder4786
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |