I have a basic script which parses and processes a set of .tmp input files:
my $extension = '.txt'; my @files = glob("*.tmp"); for my $file (@files) { open IN, '<', $file or die "$!"; $file =~ s/\..*//; my $outfile = $file.$extension; open OUT, '>', $outfile or die "$!"; while (<IN>) { next unless $. > 45; chomp $_; our(@F) = split(' ', $_, 0); @array = split(m[[:,/]+], $F[9], 0); $check = index($F[4],','); if ($check == '-1') { $ratio = $array[3] / $array[4]; print OUT "$F[0]\t$F[1]\t$F[3]\t$F[4]\t$ratio\t$array[4]\n +"; } elsif ($check > 0) { @allele = split(',', $F[4]); $ratio1 = $array[3] / $array[5]; $ratio2 = $array[4] / $array[5]; print OUT "$F[0]\t$F[1]\t$F[3]\t$allele[0]\t$ratio1\t$arra +y[5]\n$F[0]\t$F[1]\t$F[3]\t$allele[1]\t$ratio2\t$array[5]\n"; } } }
However upon running it, I am receiving the error:
Illegal division by zero at /file/pathway/script.pl line 15 <IN> line 37864Why is this occurring and how can it be fixed?
Line 37864 is the final line+1 in the first input file (i.e. it's attempting to process an empty line), so I have a feeling that it is due to the fact that in a couple of instances I am splitting one input line into two output lines (see Line 22) and this is offsetting $. but I am not sure.
In reply to Illegal division by zero error by TJCooper
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |