Hello. I'm currently teaching myself Perl and was trying to turn an awk code that I had written into Perl. I have gotten stuck on a particular part and a2p has not helped me at all. The task was to take a .csv file containing a name, assignment type, score and points possible and compute it into a weighted grade.
An example of the .csv file is:
Billy Final Final 82 100
Billy Survey WS 5 5
Bob Homework H01 19 100
Bob Homework H02 82 100
Sam Quiz Q01 95 100
Here is the code that I have:
#!/usr/bin/perl
open ($input, '<', $ARGV[0]);
$header=<$input>;
$i =0;
%data = ();
@names;
while ($line = <$input>)
{
chomp $line;
@field = split "," , $line;
$names[$i] = $field[0];
push (@{$data{$names[$i]}}, ($field[3], $field[4]));
$i = $i + 1;
}
close $input;
printf("%-8s %8s \t %s \n", "Name", "Percent", "Letter");
foreach $key ( sort keys %data )
{
@value = @{$data{$key}};
$total = 0;
$possible = 0;
$percent = 0;
$i = 0;
foreach $val (@value)
{
if($i % 2 == 0)
{
$total += $val;
}
else
{
$possible += $val;
}
$i += 1;
}
foreach $_ (keys %names) {
$Homework=($total[$_ . "Homework"] / $possible[$_ . "Homework"]) * 0.10;
$Lab =($total[$_ . "Lab"] / $possible[$_ . "Lab"]) * .30;
$Final = ($total[$_ . "Final"] / $possible[$_ . "Final"]) * .15;
$Quiz = ($total[$_ . "Quiz"] / $possible[$_ . "Quiz"]) * .40;
$Survery = ($total[$_ . "Survey"] / $possible[$_ . "Survey"]) * .05;
$percent = ($Homework + $Lab + $Final + $Quiz + $Survery) * 100;
printf "%s\t%.2f\t", $_, $percent;
if ($percent >= 90 && $percent <= 100) {
print 'A';
}
elsif ($percent >= 80 && $percent < 90) {
print 'B';
}
elsif ($percent >= 70 && $percent < 80) {
print 'C';
}
elsif ($percent >= 60 && $percent < 70) {
print 'D';
}
else {
print 'E';
}
printf("%-8s %8.2f \t %4s\n", $key, $percent, $grade);
}
}
I keep getting syntax errors on the follow part of the code:
foreach $_ (keys %names) {
$Homework=($total[$_ . "Homework"] / $possible[$_ . "Homework"]) * 0.10;
$Lab =($total[$_ . "Lab"] / $possible[$_ .[$_ . "Lab"]) * .30;
$Final = ($total[$_ . "Final"] / $possible[$_ . "Final"]) * .15;
$Quiz = ($total[$_ . "Quiz"] / $possible[$_ . "Quiz"]) * .40;
$Survery = ($total[$_ . "Survey"] / $possible[$_ . "Survey"]) * .05;
$percent = ($Homework + $Lab + $Final + $Quiz + $Survery) * 100;
The only output I am getting is Name Percent Letter and none of the data from the csv file. Any tips?