in reply to comparing an ID fom one file to the records in the second file

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.

  • Comment on Re: comparing an ID fom one file to the records in the second file
  • Download Code

Replies are listed 'Best First'.
Re^2: comparing an ID fom one file to the records in the second file
by ag88 (Novice) on Dec 02, 2017 at 11:37 UTC

    Thankyou so much for the help. I really appreciate it. But I am unable to print the line having matched ID via (print $line if exists $ids{$id};). I am trying to figure it out :(

      Hi ag88,

      please check the content of your hash. There may be invisible characters in the lines of your first file (like extra space, carriage return, etc.). The best might be to use something like the Data::Dumper module (which is core, so it should be on your machine).