in reply to Argument "" isn't numeric in numeric le (<=)

Deeply nested code should raise a red flag - things are getting out of hand and are likely to be difficult to understand. There are several ways to address the issue including removing some of the code into a subroutine and 'early exit'.

I find early exit particularly good for reducing nested if blocks. Consider this refactored version of your code using early exits:

use warnings; use strict; my %HoProPos = (1 => 1916); my %snpHash = (PF14_0747 => [1]); my @geneArr = qw(PF14_0747); while (my $line = <DATA>) { my $header = 1; chomp $line; next if $line !~ m/^Gene:\s*(.*)/; my $geneName = $1; next if ! grep (/^$geneName$/, @geneArr); while (substr ($line, 0, 3) ne "---") { $line = <DATA>; chomp $line; next if $line !~ m/(\d+)\s*([A-Z]+)\s*(\d+)-(\d+)/; my ($epitope, $epiSeq, $locBeg, $locEnd) = ($1, $2, $3, $4); foreach my $snpID (@{$snpHash{$geneName}}) { next if $locBeg > $HoProPos{$snpID} || $HoProPos{$snpID} > + $locEnd; print "Gene: $geneName\n" if $header; print "SNP $snpID found in epitope $epitope at protein pos +ition $HoProPos{$snpID}\n"; $header = undef; } } } __DATA__ Gene: PF14_0747 TABLE: Epitopes from IEDB Epitope Sequence Location on Protein Strain Confidence 26850 IKND 1914-1917 Plasmodium falciparum 3D7 Medium ------------------------------------------------------------ Gene: PF14_0711 TABLE: Epitopes from IEDB Epitope Sequence Location on Protein Strain Confidence 26850 IKND 9-12 Plasmodium falciparum 3D7 Medium ------------------------------------------------------------

Note the use of if as a statement modifier for short statements. chomp doesn't need () - by convention () are not used for Perl's built in functions.

Note too that $cnt has changed name to reflect its actual semantics and that variables are declared in the smallest sensible scope.

Although you haven't shown your file handling code, I bet you are using the two parameter version of open and possibly aren't checking the result. You certainly aren't using lexical file handles. So, you should use the three parameter version of open, check the result and use lexical file handles. Your open should then look something like:

open my $inFile, '<', $dataFileName or die "Unable to open $dataFileNa +me: $!\n";


True laziness is hard work

Replies are listed 'Best First'.
Re^2: Argument "" isn't numeric in numeric le (<=)
by hotel (Beadle) on Aug 24, 2009 at 23:19 UTC
    my $resultFile= "C://Users/kullaniciadi/Desktop/plasmodium/results.txt +"; open (OUT, ">>$resultFile") or die ("Cannot open $resultFile"); open(geneREAD, $plasData) or die ("Cannot open $plasData"); my ($geneName, $line2, $epitope, $epiSeq, $locBeg, $locEnd); my $ctr; while($line2=<geneREAD>) { $ctr=0; chomp($line2); if($line2=~ m/^Gene:\s*(.*)/) { $geneName= $1; if(grep(/^$geneName$/, @geneArr)) { while(substr($line2, 0 , 3) ne "---") { $line2=<geneREAD>; chomp($line2); if($line2=~ m/(\d+)\s+([A-Z]+)\s+(\d+)-(\d+)/) { $epitope= $1; $epiSeq= $2; $locBeg=$3; $locEnd=$4; foreach $snpID(@ {$snpHash{$geneName}})#call some +values parsed earlier in the code from an array of hashes and compare + them with locBeg & locEnd { if($locBeg <= $HoProPos{$snpID} && $HoProPos{$ +snpID} <= $locEnd) { if($ctr== 0)#this $ctr thing is used to pr +int out gene's name only once in the output file {print OUT "Gene: $geneName\n";} print OUT "SNP $snpID found in epitope $ep +itope at protein position $HoProPos{$snpID}\n"; $ctr=1; } } } } } } }

    Well, this is the part with the file handle you are mentioning. I only changed *s to +s in the inner if as I told you. I somehow do not believe that the warnings thrown pointing to dashed lines (in the .* version of the code) of the doc has anything to do with this filehandle, since it looks pretty simple and easy.

    Thank you for your help. Looks much more better with new declarations and changes. Hopefully I'll get used to it over time.