The reason the cat is that there are mulitple grades per line. So if you
do assignment only, then you will have only the last grade.

  To get 2.5, etc then you could use %4.1f to get to one decimal point.

Wags ;) ps Unknown about formats. Sorry.

-----Original Message-----
From: David Gilden [mailto:[EMAIL PROTECTED]]
Sent: Monday, July 02, 2001 12:11
To: [EMAIL PROTECTED]
Subject: Format & Sprintf


Hello, 
The following is based on the first example in Programming Perl,
and have a question about some of the syntax 
(and or why it was doen this way)

$grades{$student} .= $grade . " ";

Why not $grades{$student} = $grade;
Why the concatenation  and the extra space?

$average =  sprintf ("%3d", ($total / $scores));

I want to allow for values like 2.5, 11.5, 2, 22 ...

Finally am I using the Format correctly (assigning $~ )

Thanks
Dave


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

open (GRADES, "grades.txt") || die "Can't open grades $!\n";

while  ($line=<GRADES>){
($student,$grade)= split(" ",$line);
$grades{$student} .= $grade . " ";
}


local $~ =  grades_header;
write;


local $~ =  grades_format;

foreach $student (sort keys %grades){
$total = 0;

@grades = split (" ",$grades{$student});
foreach $grade (@grades){
$total+= $grade;  
$scores++;
}

$average =  sprintf ("%4d", ($total / $scores));

write;
# print "$student: $grades{$student}\tAverage: $average\n";
}



format grades_header =
Student     Score     Average
-----------------------------
.


format grades_format =
@<<<<<<<<<<  @<<        @<<<<
$student,$grades{$student},$average
.

--------------------------------------------
Looking for Web Talent, You found it!
portfolio: www.coraconnection.com/web/
email: [EMAIL PROTECTED]
tel/fax: (860) 231-9988
--------------------------------------------

Reply via email to