in reply to Illegal division by zero error

Your script executes line 15 if index($F[4],','); returns -1, which means if the comma isn't found. There are two obvious situations where the comma would not be found: (1) The input data has at least ten fields but there is no comma in the tenth field, or (2) The input data doesn't have ten fields. The second situation can happen, as an example, if your script encounters an empty line. Essentially, you're not handling blank lines or lines that don't conform to expectations. Even in a really sanitary data set, a blank line will often occur at the end of the file. If every line of the input ends with a \n, then the last useful data line also has a newline at the end, leaving an empty line after it. Solve the problem by skipping empty lines. Right after your chomp, put a sanity check: next unless length $_;


Dave

Replies are listed 'Best First'.
Re^2: Illegal division by zero error
by TJCooper (Beadle) on Feb 01, 2016 at 16:46 UTC
    Thanks. I had not considered that the IF criteria could be met in more than one way. I have included the recommended next unless length $_; after the chomp $_; however the same error is still returned. I've also tried next if (/^\s+$/); to no avail.

      Then the next step is to put a print statement immediately before line 15 (line 16 now that you added the next unless length line). Print the raw line, and also some of the variables holding various splits and matches. Verify that things look like you expect them to. That will help you to spot the case that you're not handling correctly.


      Dave

        Inspecting and printing @array, @F and the raw-line all show that the final entry is a blank line. The split handles all lines containing content as intended, and these errors only occur when it reaches the end of any given input file - but skipping empty lines still does not appear to work oddly.