Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: compare two files by column and return second (matching) column

by aitap (Deacon)
on Aug 06, 2012 at 19:32 UTC ( #985805=note: print w/ replies, xml ) Need Help??


in reply to compare two files by column and return second (matching) column

Firstly, a couple of advices:
my $in1 = $ARGV[0]; my $in2 = $ARGV[1]; my $fout = $ARGV[2];
It can be simplified into: my ($in1, $in2, $fout) = @ARGV;
open ONE, $in1; open TWO, $in2; open foutname, ">$fout";
You need to check for errors; it's also safer to use three-argument form of open() (imagine someone specifying '/bin/rm -rf / |' as file name). Thus,
open my $one, '<', $in1 || die "$in1: $!\n"; open my $two, '<', $in2 || die "$in2: $!\n"; open my $foutname, ">", $fout || die "$fout: $!\n";
Secondly, when you need to match something to something, think of a hash. Thus,
my %first; while (<$one>) { chomp; my @data = split /\t/; $first{$data[0]}=$data[1]; # fill the hash with the values from the f +irst file } close $one; while (<$two>) { chomp; # just search the hash for these strings exists $first{$_} && print $foutname $first{$_},'\n'; } close $two; close $foutname || warn "$fout: $!\n';
(the code is untested, feel free to ask about errors it gives)
Sorry if my advice was wrong.


Comment on Re: compare two files by column and return second (matching) column
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985805]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (17)
As of 2015-07-06 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (75 votes), past polls