blacksmith has asked for the wisdom of the Perl Monks concerning the following question:
has me wondering. I have created the file that the book lists with the names and grades and I get a message, "Illegal division by zero at grades.pl line 17, <GRADES> line 10". Now I understand if I am not allowed to divide by 0 since that would give me 0. What I was wondering was this script was written correctly for the example they are making. Thanks.#!/usr/bin/perl open(GRADES, "grades") or die "Can't open grades: $!\n"; while ($line = <GRADES>) { ($student, $grade) = split(" ", $line); $grades{$student} .= $grade . " "; } foreach $student (sort keys %grades) { $scores = 0; $total = 0; @grades = split(" ", $grades{$student}); foreach $grade (@grades) { $total += $grade; $scores++; } $average = $total / $scores; print "$student: $grades{$student}\tAverage: $average\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(ichimunki) Re: Programming Perl 3rd ed. Chapter 1
by ichimunki (Priest) on Jun 23, 2001 at 01:43 UTC | |
by blacksmith (Hermit) on Jun 23, 2001 at 02:03 UTC | |
|
Re: Programming Perl 3rd ed. Chapter 1
by converter (Priest) on Jun 23, 2001 at 01:39 UTC | |
|
Re (tilly) 1: Programming Perl 3rd ed. Chapter 1
by tilly (Archbishop) on Jun 23, 2001 at 07:42 UTC | |
|
Re: Programming Perl 3rd ed. Chapter 1
by tigervamp (Friar) on Jun 23, 2001 at 19:56 UTC |