So I'm just learning perl so I hope you don't mind me posting already, or with simple questions.
I'm trying to write a simple program that creates a list of Students' grades and average grades on assignments from a text file. I can get the first part to work and get the proper calculations for the second. However when I try to sort the arrau then my output doubles and have of it comes with an error
Use of uninitialized value in print at ./hw3.pl line 30, <STDIN> line 9.
here is the code:
#!/usr/bin/perl -w
%totals = (), %hwsum = (), %hwtimes = ();
while ($line = <STDIN>)
{
chomp $line;
($student, $assign, $grade) = split /:/, $line;
$totals{$student} += $grade;
$hwsum{$assign} += $grade;
$hwtimes{$assign} += 1;
$average{$assign} = $hwsum{$assign} / $hwtimes{$assign};
}
@all = keys %totals;
@all = sort {$totals{$b} <=> $totals{$a}} @all;
@hwall = keys %average;
@hwall = sort (%average);
print "*** STUDENT TOTALS ***\n";
foreach $st (@all)
{
print $st, ": ", $totals{$st}, "\n";
}
print "*** HOMEWORK AVG ***\n";
foreach $phw (@hwall)
{
print $phw, ": ", $average{$phw}, "\n";
}
and here is the output
*** STUDENT TOTALS ***
cindy: 24
david: 19
alice: 15
bob: 11
*** HOMEWORK AVG ***
Use of uninitialized value in print at ./hw3.pl line 30, <STDIN> line
+9.
7:
Use of uninitialized value in print at ./hw3.pl line 30, <STDIN> line
+9.
8:
Use of uninitialized value in print at ./hw3.pl line 30, <STDIN> line
+9.
8:
hw1: 8
hw2: 8
hw3: 7
Thanks for any help
--Jeeves
20050413 Janitored by Corion: Added code tags
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.