---- #!/usr/bin/perl

# This is the GPA calculation routine for the
# Eduadmin system again.
#

$ifile = 'gpa.dat';

open (INF, "<$ifile") || die "Can't open file $ifile:$!\n";

$cp_total = 0;

$id = 0;

$temp = 0;

LINE: while (<INF>) { ($id, $grade, $cp) = split; if ($grade =~ /NG/i ||
$grade =~ /YL/i ||
$grade =~ /DF/i ||
$grade =~ /\bY\b/i ||
$grade =~ /W/i ||
$grade =~ /T/i) {next LINE;}


$cp_total += $cp; if ($grade =~ /\bA\b/i) { $a6++ if $cp == 6; $a4++ if $cp == 4; $a3++ if $cp == 3; $a2++ if $cp == 2; }
if ($grade =~ /\bB\b/i) { $b6++ if $cp == 6; $b4++ if $cp == 4; $b3++ if $cp == 3; $b2++ if $cp == 2; }


if ($grade =~ /\bC\b/i) { $c6++ if $cp == 6; $c4++ if $cp == 4; $c3++ if $cp == 3; $c2++ if $cp == 2; }

if ($grade =~ /\bD\b/i) { $d6++ if $cp == 6; $d4++ if $cp == 4; $d3++ if $cp == 3; $d2++ if $cp == 2; }

      if ($temp == $id) {
         next LINE;
       }
      else {

        $wcp = ($a6*4.0*6.0 + $a4*4.0*4.0 + $a3*4.0*3.0 + $a2*4.0*2.0) +
               ($b6*3.0*6.0 + $b4*3.0*4.0 + $b3*3.0*3.0 + $b2*3.0*2.0) +
               ($c6*2.0*6.0 + $c4*2.0*4.0 + $c3*2.0*3.0 + $c2*2.0*2.0) +
               ($d6*1.0*6.0 + $d4*1.0*4.0 + $d3*1.0*3.0 + $d2*1.0*2.0);
                            $gpa = $wcp / $cp_total;

           $temp = $id;
           $cp_total = 0;
           next LINE;
      }

} # end of while

use hashes of hashes.........

#Lets first store the hashes
while(<INF>){
($id, $grade, $cp) = split(/\s+/);
#I am removing the .00 for convenience. You can preserve it and make changes accordingly
$cp =~ s/(\d)\.00/$1/;
#No need for label.. lets put everything in one search
next if $grade =~ /(NG|YL|DF|\bY\b|W|T)/ ;
#This is the best part.. I am creating hashes of hashes of hashes. The hashes takes care of uniqueness.
$student{$id}->{$grade}->{$cp}++;
#Simple hash for sum of credit points
$cp_total{$id} += $cp;
}


#Now calculate the weighted credit points for each student..
foreach $id ( keys %student ){
#Even this can be improved by using more foreaches. But lets keep it like this for easy readability
$wcp = ($student{$id}->{A}->{6}*24 + $student{$id}->{A}->{4}*16 + $student{$id}->{A}->{3}*12 + $student{$id}->{A}->{2}*8)+
($student{$id}->{B}->{6}*18 + $student{$id}->{B}->{4}*12 + $student{$id}->{B}->{3}*9 + $student{$id}->{B}->{2}*6)+
($student{$id}->{C}->{6}*12 + $student{$id}->{C}->{4}*8 + $student{$id}->{C}->{3}*6 + $student{$id}->{C}->{2}*6)+
($student{$id}->{D}->{6}*6 + $student{$id}->{D}->{4}*4 + $student{$id}->{D}->{3}*3 + $student{$id}->{D}->{2}*2);
#Calculate the gpa and store it for each student
$gpa{$id} = $wcp/$cp_total{$id};
}


#Lets print it now. We could have done it in the above foreach also ..
foreach $id (keys %student){
   print "GPA for $id : $gpa{$id}\n";
}

BTW, I got these values
GPA for 216 : 1.98
GPA for 386 : 1.40

Although it is correct for 386.. it does not matches for id 216. Are you sure gpa for 216 is correct.

Hope you got the idea of how you can use hashes to make the program short and simple.

--
Ankur



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to