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 37864

Why 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.