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

Hi, welcome. It's a FAQ, there are tons of threads about it in this monastery. Have you searched? Do you know what your code does, or did you copy it from somewhere without really understanding it? As a beginner, have you worked through perlintro yet? You'll also need perlrequick if you are doing text matching.

You must always use strict; and use warnings; at the top of your code.

For example, warnings would have told you that you were trying to read from a closed filehandle.

(If you close the comparison filehandle first time through the loop the rest of the lines in the id file never have a chance to match.)

Don't copy this. (edit: because it won't work, as Laurent_R points out below. I was trying to show some errors in your code, (see above), but as others have noted your overall approach is wrong to begin with for your task.) Try to spot the differences. Ask if you have any questions:

#!/usr/bin/perl
use strict; use warnings;

my $file_id = './BreastCnAPmiRNAsID.txt';
open( my $FILEID, '<', $file_id ) or die "Died: cannot open $file_id: $!";

my $file_comp = './tarbaseData.txt';
open( my $FILECOMPARE, '<', $file_comp ) or die "Died: cannot open $file_comp: $!";

while ( my $id = <FILEID> ) {
    chomp $id;

    while ( my $comp = <FILECOMPARE> ) {
	chomp $comp;
	print "$id\n";
	if ( $comp =~ /$id/ ) {
	    print "\t$_\n";
	} else {
            print "\tno match\n";
        }
    }
}

close $FILEID;
close $FILECOMPARE;
__END__
(untested)

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: comparing an ID fom one file to the records in the second file
by Laurent_R (Canon) on Dec 01, 2017 at 18:52 UTC
    hi 1nickt,

    unless I missed something, I think that this isn't gonna work. When you read the first line of FILEID, you read the whole FILECOMPARE filehandle, and you won't have any data left to read from the second file for the the next lines of the first file. Besides, even if you fixed it to get back to the beginning of the first file, the solution would be quite inefficient, because it would be reading the second file again and again for each input line of the first file. It would also print scores of "no match" to the output, even when there is actually a match.

      Quite likely, I didn't test it, and said "don't copy this" ... was just correcting some errors I saw in the OP. As you know well this is not the right solution to begin with. I'll update my node, thanks.

      The way forward always starts with a minimal test.
        Yes, 1nickt, I gathered that you tried to fix the syntax, but probably overlooked the fact that the OP's semantic approach (the algorithm) was also wrong.
Re^2: comparing an ID fom one file to the records in the second file
by ag88 (Novice) on Dec 02, 2017 at 11:45 UTC

    Thankyou for pointing out the mistakes in the code. I am working on it. No, I did not copy the code from somewhere else. I did it myself. Thats perhaps the reason it is not working :P