in reply to comparing an ID fom one file to the records in the second file
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:
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.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;
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.
|
|---|
| 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 | |
by Laurent_R (Canon) on Dec 02, 2017 at 11:45 UTC |