Originally posted by: notfred
Just for the hell of it, I beleive this is the entire solution to the problem in the first post. I didn't see anything about averages in there, so I left them out.
#!/usr/bin/perl
use strict;
# Get filename and list of subjects.
print "Enter filename: ";
my $filename = <>;
open FILE, "$filename";
my $subjects = <FILE>;
chomp $subjects;
$subjects =~ s/Subjects-\s*//;
$subjects =~ s/\s*,\s*/,/g;
my @subjects = split /,/, $subjects;
# Initialize an array of hashes
my @grades;
for my $ii(0..$#subjects){
my %newhash;
push @grades, \%newhash;
}
# Process each student
while(<FILE>){
# Get name
$_ =~ s/^(\D+)//;
my $name = $1;
# Get scores
my @scores = split /\s+/, $_;
# For each subject, insert the score into the proper hash in @grades
for my $subject(0..$#subjects){
${$grades[$subject]}{$scores[$subject]} = $name;
}
}
# Print each subject
foreach my $subject(0..$#subjects){
print "$subjects[$subject]\n";
foreach my $score(reverse sort keys %{$grades[$subject]}){
print ${$grades[$subject]}{$score}, ": $score\n";
}
print "\n";
}