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

Store the ID's as hash keys and use exists to match records in the data file

#!/usr/bin/perl use strict; use Data::Dumper; my %ID = (); my $fileID = 'BreastCnAPmiRNAsID.txt'; open FILEID, '<', $fileID or die "cannot open $fileID"; while (<FILEID>){ chomp; $ID{$_}=1 if $_; } close FILEID; #print Dumper \%ID; my $fileCompare = 'tarbaseData.txt'; open FILECOMPARE, '<', $fileCompare or die "cannot open $fileCompare"; while(<FILECOMPARE>){ chomp; my @col = split "\t",$_; print "$col[2]\n"; if (exists $ID{$col[2]}){ print $_; } } close FILECOMPARE;
poj
  • 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:27 UTC

    Thankyou so much for the help poj. Much appreciated. and I understand the logic how to do this type of task i.e., using the hash and storing IDs as keys etc. But there still exists one problem. The code is not comparing the hash key (ID) with $col2 and is not printing the related line :(. I commented out the (print "$col2") and it is printing nothing. I am trying to figure this out in the mean time.

      Check that your ID file does not have hidden spaces, tabs etc. Ensure 'clean' data by adding a regex

      while (<FILEID>){ chomp; s/[\s]//g; $ID{$_}=1 if $_; }
      poj

        Thanksalot poj. It worked. I have to run this script on multiple files now. I will get back to you for further help if needed. Thanksalot again :)