my %to_match = (); # hold things to match in here my $output = ''; # hold what we want to print # open first file while () { my $element = (split(/\t/, $_))[$column1]; $to_match{$element} = 1; } # open second file while () { my $element = (split(/\t/, $_))[$column2]; if (defined($to_match{$element})) { $output .= $_; } } # open output file and display results