• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

PERL GURU I NEED YOUR HELP?

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
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";
}



Hey fred

how would i complete this and calculate th adverages

with this


$average = $total / $scores;
print "$student: $grades{$student}\tAverage: $average\n"
 
Back
Top