Thread zentrierte Darstellung mit printf möglich? (20 answers)
Opened by Duff at 2007-09-19 13:41

Duff
 2007-09-19 17:03
#99688 #99688
User since
2006-10-06
283 Artikel
BenutzerIn

user image
Hallo,

verstehe das ganze wohl doch noch nicht so ganz.
Habe folgendes gemacht:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
my $length;
my $width=4;
sub print_formatted{
        my ($max,$t1,$t2,$t3,$t4,$t5,$t6) = @_;

        # berechne den linken Rand $lRand
        my ($lRand) = int( ($max - length $t4) / 2 );
        # berechne den rechten Rand $rRand
        my $rRand = $max - $lRand - length $t4;

        my $format = "%6d %-12s %-12s %*s%s%*s %-12s %10d\n";

        print sprintf $format, $t1,$t2,$t3,$lRand,' ',$t4,$rRand,' ',$t5,$t6;
}


print sprintf "%6s %-12s %-12s %s %-12s %10s\n", "Spalte1", "Spalte2", "Spalte3", "Spalte4", "Spalte5", "Spalte6";
      
        for(my $i=0; $i<=$#array; $i=$i+6) {
                print_formatted($width, $array[$i], $array[$i+1], $array[$i+2], $array[$i+3], $array[$i+4], $array[$i+5]);
        }


Sobald ich aber die Variable width auf 8 oder so setze, funktioniert die ganze Ausgabe nicht mehr sauber bzw. die Spalte mit $t4 wird nicht mehr zentriert, sondern nach rechts verschoben.
D'OH
Daniel

View full thread zentrierte Darstellung mit printf möglich?