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;

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.