Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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 wandering the Monastery: (10)
As of 2014-12-19 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (75 votes), past polls