Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985790]
[ambrus]: GotToBTru: wait, you tell only the atom totals of what you want? Is that like ordering food in a restaurant by telling only the nutrient amounts you need, or
[ambrus]: like when a medieval scientist supposedly proves his priority inventing something by having previously published an anagram of a thousand letters long summary of the invention?
[choroba]: Progress! Nowadays, you can write a Perl script to generate such an anagram!
[GotToBTru]: i like to order my chemicals a-la-carte ambrus ;)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2016-12-06 13:07 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (104 votes). Check out past polls.