Try %5.1f in your sprintf
The first number is the width of the field, in this case your format, @<<<<,
is specifying 5 wide.
Alernatively you could make your format @##.# and get rid of the sprintf
$average = $total / $scores;
----- Original Message -----
From: "David Gilden" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, July 06, 2001 11:06 AM
Subject: padding numbers with spaces
> Good day,
> Thanks in advance to of you great folks on this list!
> Dave G.
>
> I am trying with out much success to get the following to happen:
>
> Using this statement:
>
> $average = sprintf ("%2.1f", ($total / $scores));
>
> I want my numbers to line up like so: [note the left padding with the ' ']
>
> fred 72 18.0 # with or with out the trailing '0'
> joan 15 3 # would like to know how to accomplish both!
> john 21 3.5
> ----
>
> this what you might get after running the script as is:
> ----------------------------------------------
> fred 72 18.0
> joan 15 3.0
> john 21 3.5
>
>
> --- script --
>
>
> open (GRADES, "grades.txt") || die "Can't open grades $!\n";
>
> while ($line=<GRADES>){
> ($student,$grade)= split(" ",$line);
> $grades{$student} .= $grade . " ";
> }
>
> foreach $student (sort keys %grades){
> $total = 0;
>
> @grades = split (" ",$grades{$student});
> foreach $grade (@grades){
> $total+= $grade;
> $scores++;
> }
>
> $average = sprintf ("%2.1f", ($total / $scores));
> write;
> }
>
>
>
> format STDOUT_TOP =
> Page @<<
> $% # page number variable
> Student Score Average
> -----------------------------
> .
>
>
> format STDOUT =
> @<<<<<<<<<< @<< @<<<<
> $student,$grades{$student},$average
> .
>
>