perl script not giving output
The following program is suppose to read rs ID's of SNPs from the file 'testID' and then search them from file 'testSNP' and sends the output to testSNP.out. I through multiple print statements have debugged the program and found no error but it is not generating the output. what is the problem with this code?
#!/usr/bin/perl
use warnings;
use strict;
my $rs_id;
open (my $id_file, '<testID') or die "Could not open the file: $!\n";
while ($rs_id = <$id_file>) {
findVariants($rs_id); }
close $id_file;
print "Done\n";
sub findVariants {
my ($rs_num) = @_;
open (my $in_file, '<testSNPs') or die "Could not open the file: $!\n";
my @snp = <$in_file>;
my @result= grep /$rs_num/, @snp;
print @result;
open (my $out_file, '>>testSNPs.out');
print $out_file @result;
close $out_file;
close $in_file;
}
• 3,291 views
•
link
0 answers
No answers yet.
Log in to answer this question.
Share the contents of
testIDso that I can run the same at my end and troubleshoot. Will be happy to assistI have added the 'chomp' statement and now it is working fine.
One thing to look for is that when reading from a file, the variable includes the end of line character(s), so in your case $rs_id contains the whole line including the \n character (assuming Linux), e.g. $rs_id contains 'rs1234\n'. So later when trying to match, you still have this end-of-line character. Try:
yes it works fine now. Thank you