Try adding the second and third line below:
$average =  sprintf ("%2.1f", ($total / $scores)); #original line
$average =  ' ' x (2 - int(log($average)/log(10))) . $average;
$average =~ s/\.0+//;

The second line effectively takes the log base 10 of the average, and adds 
additional whitespace to pad out the number.  The third line, if you'd like, 
will take off any the decimal and trailing zeroes.

Also, you may want to add the second line below:
$total = 0;
$scores = 0;

So that the people later in the file aren't penalized by the other tests 
they didn't take ;)

-Nick

>From: David Gilden <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: padding numbers with spaces
>Date: Fri,  6 Jul 2001 13:06:00 -0400
>
>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
>.
>

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

Reply via email to