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";
|
|---|
| 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 |