Hello. I'm self teaching myself and coded a program but I keep getting a illegal division by 0 error. I know what it means but I don't know where I am messing up.
Here is the code:
#!/usr/bin/perl
@lines = <>;
my %earned;
my %possible;
for ($i = 1; $i < scalar @lines; $i++) {
$line = @lines[$i];
chomp($line);
@fields = split(/,/, $line);
$earned{@fields[0]} = $earned{@fields[0]} + @fields[3];
$possible{@fields[0]} = $possible{@fields[0]} + @fields[4];
}
print "Student\tPercent\tLetter\n";
for $student (keys %earned) {
$homework = ($earned{$_,"Homework"} / $possible{$_,"Homework"}*.10). "\n"; #This is where I am getting the error.
$lab = ($earned{$_,"Lab"} / $possible{$_,"Lab"}*.30) . "\n";
$final = ($earned{$_,"Final"} / $possible{$_,"Final"}*.15) . "\n";
$quiz = ($earned{$_,"Quiz"} / $possible{$_,"Quiz"}*.40) . "\n";
$survey = ($earned{$_,"Survey"} / $possible{$_,"Survey"}*.30) . "\n";
$score = ($homework + $lab + $final + $quiz +$survey);
if ($score >= 90){
$letter = "A";
}
elsif ($score >=80 && $score < 90){
$letter = "B";
}
elsif ($score >= 70 && $score < 80){
$letter = "C";
}
elsif ($score >= 60 && $score < 70){
$letter = "D";
}
else {
$letter = "E";
}
printf("%s\t%.2f\t%s\n", $student, $score, $letter);
}
Here is the sample data from the .csv file that this script is calculating.
Sam Homework 95 100
Jim Quiz 60 100
Pam Homework 55 100
Sam Final 80 100
Sam Lab 5 5