mrglaeser has asked for the wisdom of the Perl Monks concerning the following question:
and here is the output#!/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"; }
*** 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
20050413 Janitored by Corion: Added code tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: New to Perl, getting funny results when sorting an Array
by davido (Cardinal) on Apr 13, 2005 at 03:00 UTC | |
by eibwen (Friar) on Apr 13, 2005 at 07:08 UTC | |
|
Re: New to Perl, getting funny results when sorting an Array
by rev_1318 (Chaplain) on Apr 13, 2005 at 07:38 UTC |