ejbiers has asked for the wisdom of the Perl Monks concerning the following question:
I have two files. File A contains 2 columns (tab separated) and File B contains 1 column.
ex. File A
name1 xxxxx
name2 yyyyy
name3 zzzzz
name4 aaaaa
name5 bbbbb
File B
name3
name5
name1
What I'd like to get in an output file is:
zzzzz
bbbbb
xxxxx
I am a novice at perl and have been trying to modify the following script (designed to count the number of occurrences) to meet my needs with no success. Could someone suggest how to modify this script? Thanks for any help you can give!
unless (@ARGV == 3) { print "Use as follows: perl program.pl in1.file in2.file output.fi +le\n"; die; } my $in1 = $ARGV[0]; my $in2 = $ARGV[1]; my $fout = $ARGV[2]; open ONE, $in1; open TWO, $in2; open foutname, ">$fout"; my %hash1; my @hit; while (<ONE>){ chomp; my @hit = split(/\t/, $_); #start them as "0" for "no duplicates" $hash1{$hit[0]}=0; } close ONE; my @col; while (<TWO>){ chomp; my @col = split(/\t/, $_); #increment the counter if %hash1 has what we're looking for. ++$hash1{$col[0]} if(exists($hash1{$col[0]})); } my @dups = grep { $hash1{$_} > 0 } keys %hash1; for my $k (@dups) { print foutname "$k\t$hash1{$k}\n"; } close TWO; close foutname;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: compare two files by column and return second (matching) column
by Kenosis (Priest) on Aug 06, 2012 at 20:36 UTC | |
by ejbiers (Initiate) on Aug 07, 2012 at 13:45 UTC | |
by Kenosis (Priest) on Aug 07, 2012 at 15:28 UTC | |
|
Re: compare two files by column and return second (matching) column
by nemesdani (Friar) on Aug 06, 2012 at 18:26 UTC | |
|
Re: compare two files by column and return second (matching) column
by BillKSmith (Monsignor) on Aug 06, 2012 at 19:13 UTC | |
|
Re: compare two files by column and return second (matching) column
by aitap (Curate) on Aug 06, 2012 at 19:32 UTC | |
|
Re: compare two files by column and return second (matching) column
by abualiga (Scribe) on Aug 06, 2012 at 19:41 UTC |