I've been struggling with what should be a trivial problem. Perl is such a rich language that in formulating the logic, I've actually
became confused. Partly because I took a C approach when I know that a veteran Perl programmer would write only a few lines to achive the same result. (The challenge coming from 'The Camel' 3rd ed. pp.119, 120).
I have a fairly large file with the records in the format:
ID1 Grade Credit ID1 Grade Credit ID1 Grade Credit . . ID2 Grade Credit ID2 Grade Credit ID2 Grade Credit . . ID3 Grade Credit ID3 Grade Credit ID3 Grade Credit . etc,
The IDs are the student identication numbers. The Grades stand for letter grades A, B, C, D, F, NG, YL, Y, W, T, DF.
Credit are the credit points for the courses and take on the values 2, 3, 4, 6.
To calculate the grade point average, I use the formula:
$gpa = $wcp / $cp_total
where $gpa = Grade Point Average, $wcp = weighted credit points, $cp_total = sum of credit points for the courses.
The weighted credit points is given by the formula:
$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);
One of the conditions is that only A, B, C, D, F grades are to be used in the calcuation of the GPAs.
To do that I need to know how many As, a student has for a course, say worth, 6 points. This is shown by the variable
$a6. For a 4-point course, $a4, etc, Each letter is weighted with A=4, B=3, C=2, D=1, F=0.
Below is one of just of several coding that I have developed so far. Somewhere the while, unless, do or until will do the job but I haven't quite figure out their positions in the program.
What is the Perl idiom for doing such a calculation?
For the sample data:
id 386, $gpa = 35/25 = 1.40
id 216, $gpa = 92/53 = 1.73
Thanks in advance for the help.
Alfred Vahau
------------- One of my codes which does not produce correct results -----------
---- #!/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
The sample data is:
386 F 3.00 386 C 3.00 386 C 3.00 386 D 2.00 386 D 3.00 386 D 3.00 386 B 3.00 386 C 3.00 386 F 2.00 216 C 2.00 216 C 2.00 216 C 2.00 216 YL 4.00 216 YL 2.00 216 C 2.00 216 D 2.00 216 C 2.00 216 YL 4.00 216 YL 2.00 216 C 2.00 216 C 2.00 216 D 2.00 216 C 4.00 216 C 2.00 216 C 3.00 216 C 3.00 216 C 3.00 216 C 3.00 216 C 2.00 216 C 3.00 216 F 3.00 216 D 3.00 216 F 3.00 216 C 3.00
--
Perl - "... making the easy jobs easy,
without making the hard jobs impossible."
'The Camel', 3ed
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>