in reply to Finding Minimum Value
Hello jimmy88, and welcome to the Monastery!
Aside from the problem with $mingrade, which atcroft has already addressed, there is a subtle logic error in your script which is masked by the fact that in the data file “grades.txt” the final line’s “exam” happens to be the value of the total number of exams. Change the order of this file to this:
... 222223 5 96 244668 5 95 355779 5 94 122334 1 98
and the averages will all be wrong. The reason for this is the way Perl aliases the variable in a foreach loop: after the foreach $exam (1..$lastexam) { loop has completed, $exam reverts to the value it had before the loop was entered. In this case, you can fix the problem by changing the line:
printf "%8d",$total / $exam;
to:
printf "%8d",$total / $lastexam;
Note also that this script would be greatly improved by the addition of the lines:
use strict; use warnings;
at its head. Converting global variables to lexicals will require some thought (to determine the correct placement of each my declaration), but the effort expended now will be more than repaid by the debugging time you will save down the track. (To silence the warnings, you will also need to initialise some of the variables.)
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|