This is for all of you who have read the Programming Perl book. In trying to pick the code apart before proceeding with the text I came across this. I understand some VERY BASICs of programming, and I am just beginning this book in hope that I will be able to get more in depth and be able to effectively write codes in half the space in which I have written them before (ie.
Document Maintenance Script). The first example in the book :
#!/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";
}
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.
Blacksmith
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.