Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by nemesdani (Friar)
on Aug 06, 2012 at 18:26 UTC ( #985790=note: print w/ replies, xml ) Need Help??


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

I have some general advice for you, but I'll wait till I'm sober. Till then:

use strict; use warnings; my %data; open (DATA1, "<", "data1.txt") or die "cannot open file"; while (<DATA1>) { my @line = split (/\t/, $_); $data{$line[0]} = $line[1]; } my @k = keys %data; foreach my $key (@k) { print "$key, $data{$key}\n"; } close DATA1; open (DATA2, "<", "data2.txt") or die "cannot open file"; print "found:\n"; while (<DATA2>) { chomp; if (exists $data{"$_"}) {print "$data{$_}\n";} close DATA2;
This doesn't check duplicates, is too verbose, but I hope it'll help.

I'm too lazy to be proud of being impatient.


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (21)
As of 2015-07-30 20:22 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 (273 votes), past polls