Basic Perl Scripting Exercise 2 To be handed in: Thursday 11th of October In this exercise we will process the contents of a file. Carefully read the average example (page 17 in the 3.edition, page 10 in the 2. edition of Programming Perl, Wall). It is given below. Try to understand what is going on in the script. Grade example: Suppose you had a set of scores for each member of a class you are teaching. You'd like a combined list of all the grades for each student, plus their average score. You have a text file (imaginatively named grades) that looks like this: Noel 25 Ben 76 Clementine 49 Norm 66 Chris 92 Doug 42 Carol 25 Ben 12 Clementine 0 Norm 66 ... You can use the following script to gather all their scores together, determine each student's average, and print them all out in alphabetical order. This program assumes, rather naively, that you don't have two Carols in your class. That is, if there is a second entry for Carol, the program will assume it's just another score for the first Carol (not to be confused with the first Noel). By the way, the line numbers are not part of the program, any other resemblances to BASIC notwithstanding. 1 #!/usr/bin/perl 2 3 open(GRADES, "grades") or die "Can't open grades: $!\n"; 4 while ($line = ) { 5 ($student, $grade) = split(" ", $line); 6 $grades{$student} .= $grade . " "; 7 } 8 9 foreach $student (sort keys %grades) { 10 $scores = 0; 11 $total = 0; 12 @grades = split(" ", $grades{$student}); 13 foreach $grade (@grades) { 14 $total += $grade; 15 $scores++; 16 } 17 $average = $total / $scores; 18 print "$student: $grades{$student}\tAverage: $average\n"; 19 } Now before your eyes cross permanently, we'd better point out that this example demonstrates a lot of what we've covered so far, plus quite a bit more .... . Your Exercise: Use the following list and write a program close to the above example, which gives you the average grade for females and for males. The below list will be send out electronically also, to facilitate processing. Name Grade Gender Johannes 25 male Kathrin 26 female Polina 44 female Kai 20 male Anna 33 female Valentin 40 male Peter 23 male Mathias 30 male Barbara 34 female Hand in your script and the output. Good luck!