[Buffalo-pm] Syntax error at hash assignment.

Shankar, Ganesh Ganesh.Shankar at RoswellPark.org
Wed Sep 1 10:37:11 CDT 2004


I'm new to perl and learning by running examples. I'm using a script from Programming Perl, 2nd edition, 2nd chapter. There's a script involving getting the average grades of students (http://www.oreilly.com/catalog/pperl2/excerpt/ch01.html#PERL2-CH-1-SECT-3). 
Essentially students are listed in the first column and their scores are listed in the second column. The student can be listed multiple times in the first column. The script should gather all the scores for each student and average them. Then print out the student's name once and their corresponding average.

I have to do something analogous in my microarray data analysis. I copied the script, added strict and warning, assigned scope to the variables and ran into a syntax problem on this line: my $grades{$student} .= $grade . " ";. I'm trying to figure out how the program works but I can't see the syntax error. 

Thanks for any help.



 #!/usr/bin/perl
use strict;
use warnings;

open( GRADES, "grades" ) or die "Can't open grades: $!\n";
while ( my $line = <GRADES> ) {
    ( my $student, my $grade ) = split( '\t', $line );
    my $grades{$student} .= $grade . " ";
}

foreach $student ( sort keys my %grades ) {
    my $scores = 0;
    my $total  = 0;
    my @grades = split( '\t', $grades{$student} );
    foreach $grade (@grades) {
        $total += $grade;
        $scores++;
    }
    my $average = $total / $scores;
    print "$student: $grades{$student}\t Average: $average\n";
}




More information about the Buffalo-pm mailing list