What you did will help prevent typos, but now you're using lexical variables as globals which is not a good idea. You should declare lexicals so they stay inside the relevant scope. Also got rid of double quotes where interpolation is not needed, and lost any unneccessary parenthesis, and specify the mode for open:
open my $fh, '<', 'grades' or die "Can't open grades: $!\n";
while (my $line = <$fh>) {
my ($student, $grade) = split ' ', $line;
$grades{$student} .= $grade . ' ';
}
foreach my $student (sort keys %grades) {
my ($scores, $total) = (0, 0);
my @grades = split ' ', $grades{$student};
foreach my $grade (@grades) {
$total += $grade;
$scores++;
}
my $average = $total / $scores;
print "$student: $grades{$student}\tAverage: $average\n";
}
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.