Whittling down code to the bare minimum needed to show a problem is an essential part of the art of programming. Learning this art will save you oodles of time debugging time, teach you a great deal about how code and data interconnect, and make it much easier for you to get help on problems that really have you stuck.
There are no cookbook recipes but a few guidelines may help you in the future whether you are posting a problem here or just trying to find out a solution on your own:
- Work on a copy of the original code. This way you will have less fear of messing things up.
- Be experimental! Remove things, add them back. See what happens. Continue to remove things until you stop having the problem or run into compilation errors. Put them back until the problem reappears. Repeat this process over and over until you find the right combination of things to remove.
- Get rid of the command line processing. Unless the interpretation of command line arguments is the problem, you can reduce the amount of code by replacing command line/ui processing code with code that assigns hard coded values to the variables set via the command line or interactive user interface. Using hard coded values also makes it easier for others to reproduce the problem, should you decide to post. In your case, that would mean assigning fixed values to $inputfile, $outputfile and the $opt_... variables.
- Reduce your input data to a single line (or two). Usually you can demonstrate the problem using just one or two lines of data. In your case you needed just two lines that should have had a total non-zero score but didn't.
- Follow your code flow. In your reduced sample you want only the code that actually impacts the problem output. This means you will have to visually trace the flow of your code. This can be hard to do if you are a new programmer because efficiently following code flow requires some fluency in reading code. Fortunately, the more you do the faster you will get and the less awkward it will feel. Experimenting by commenting out and adding back in code can help confirm your guesses about how each bit of code is connected to the next.
In your case, your program contains both score totals and summary statistics (min, max, means), but your problem is only with the totals, so you would keep only the code needed to calculate and display totals and eliminate all code that contributes only to min,max, or means.
Best, beth