Hi ag88,

The typical way to solve this type of problem is to first read the second file, store each line in an array, using the ID as a key and the full line as a value. Then close that file. They you read the first file and, for each line, lookup the hash to see if you find the ID. If you do, print out the hash value to the output file.

Here, however, since you only want to keep the matching items, it would be slightly simpler to do it the other way around: read the first file and store the ID in a hash (as hash keys, the hash value can be anything, for example number 1). Close that file once this is done. Then open the second file, read it line by line, extract the ID from the line, and print that line to the output file if the ID is found in the hash.

Something like this:

use strict; use wanings; my %ids; open my $IDS, "<", "BreastCnAPmiRNAsID.txt" or die "cannot open file B +reastCnAPmiRNAsID.txt $!"; while (<$IDS>) { chomp; $ids{$_} = 1; # populating the hash } close $IDS; open my $FILECOMPARE, "<", "tarbaseData.txt" or die "cannot open tarba +seData.txt $!"; while (my $line = <$FILECOMPARE>) { my $id = (split /\s+/, $line)[2]; # extract the ID (third field +) print $line if exists $ids{$id}; # print line if hash lookup i +s successful } close $FILECOMPARE;
This prints the result to the standard output. You'll have to open a third file in write mode and print to it if you want the result in another file.

Update: poj typed faster than me (or started to type earlier), our solutions are quite similar.

Update: Fixed missing quotes in the name of the first file. Thanks to 1nickt for pointing out this typo.


In reply to Re: comparing an ID fom one file to the records in the second file by Laurent_R
in thread comparing an ID fom one file to the records in the second file by ag88

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.