[Buffalo-pm] Syntax error at hash assignment.

Kevin Eye eye at buffalo.edu
Wed Sep 1 10:59:22 CDT 2004


I believe the problem is your use of "my" on that line. You need to use  
"my" to declare the entire hash, but not to declare each key of the  
hash. You also don't want the "my" on the foreach line. So change it to  
something like this:

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

foreach $student ( sort keys %grades ) {
...


  - Kevin


On Sep 1, 2004, at 11:37 AM, Shankar, Ganesh wrote:

> 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";
> }
>
>
> _______________________________________________
> Buffalo-pm mailing list
> Buffalo-pm at mail.pm.org
> http://mail.pm.org/mailman/listinfo/buffalo-pm
>


--
Kevin Eye
Web Applications Developer
Creative Services and Marketing
University at Buffalo
330 Crofts Hall
Buffalo, NY 14260
eye at buffalo.edu
phone (716) 645-5000 x1435
fax (716) 645-3765



More information about the Buffalo-pm mailing list