Thread Formatierte Ausgabe / Platzhalter ... aber wie ? (23 answers)
Opened by cherished at 2008-06-26 11:29

hlubenow
 2021-10-07 09:55
#193623 #193623
User since
2009-02-22
875 Artikel
BenutzerIn
[default_avatar]
Um die Daten von Kunden, Produkten und Aufträgen zu organisieren, eignen sich übrigens Objekte wunderbar. Ich würde die Daten also so organisieren. Allerdings liefert die Dein Programm leider nicht so, das ist also nur theoretisch. Aber wäre schon cool:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/perl

use warnings;
use strict;

package Customer {

    sub new {
        my $classname = shift;
        my $self = {};
        $self->{name} = shift;
        return bless($self, $classname);
    }
}

package Product {

    sub new {
        my $classname = shift;
        my $self = {};
        $self->{price} = shift;
        return bless($self, $classname);
    }

}

package Order {

    sub new {
        my $classname = shift;
        my $self = {};
        $self->{id}        = shift;
        $self->{quantity}  = shift;
        $self->{customer}  = shift;
        $self->{product}   = shift;
        $self->{addtlinfo} = shift;
        return bless($self, $classname);
    }
}

sub toEUR {
    my $n = shift;
    $n =~ s/\,/./;
    if ($n =~ /\,/) {
        print "\nError: Too many commas in '$n'.\n\n";
        exit 1;
    }
    $n = sprintf("%.02f", $n);
    $n =~ s/\./,/;
    if ($n =~ /\./) {
        print "\nError: Too many points in '$n'.\n\n";
        exit 2;
    }
    return $n;
}

sub print_formatted {
    # @fields: $quantity, $id, $name, $price, $addtlinfo:
    my @fields = @_;
    $fields[3] = toEUR($fields[3]);
    my @formats = ([5, "r"], [7, "r"], [40, "l"],
                   [6, "r"], [20, "r"]);
    my $separator = " ";
    my $s = "";
    my $i;
    my @v;
    my $spaces;
    for $i (0 .. $#formats) {
        @v = @{$formats[$i]};
        if (length($fields[$i]) > $v[0]) {
            print "\nError: Length of '$fields[$i]' shouldn't be bigger than $v[0].\n\n";
            exit 3;
        }
        $spaces = " " x ($v[0] - length($fields[$i]));
        if ($v[1] eq "r") {
            $s .= $spaces;
            $s .= $fields[$i];
        } else {
            $s .= $fields[$i];
            $s .= $spaces;
        }
        if ($i == 3) {
            $s .= " Euro  ";
        } else {
            $s .= $separator;
        }
    }
    print "$s\n";
}

my @customers = ();
push(@customers, Customer->new("Mueller"));
push(@customers, Customer->new("Meier"));
push(@customers, Customer->new("Schulze"));
push(@customers, Customer->new("Schmidt"));
push(@customers, Customer->new("Schulz"));
push(@customers, Customer->new("Scholz"));

my @products = ();
push(@products, Product->new(5));
push(@products, Product->new(250.75));
push(@products, Product->new(3));
push(@products, Product->new(125));
push(@products, Product->new(25));
push(@products, Product->new(0.25));

my @orders = ();
push(@orders, Order->new(12345, 10, $customers[2], $products[0], "Angebot"));
push(@orders, Order->new(12346, 25, $customers[1], $products[4], "Aktion"));
push(@orders, Order->new(12347, 75, $customers[0], $products[5], "Reduziert"));
push(@orders, Order->new(12348, 100, $customers[3], $products[1], "Sonderangebot"));
push(@orders, Order->new(12349, 2, $customers[4], $products[2], "Billig"));
push(@orders, Order->new(12350, 14, $customers[5], $products[3], "Lieferung"));

print "\nMenge Produkt Name" . " " x 36 . "  Preis(Euro) " . " " x 16 . "Info\n";
print "========================================================================================\n";

my $order;
for $order (@orders) {
    print_formatted($order->{quantity},
                    $order->{id},
                    $order->{customer}->{name},
                    $order->{product}->{price},
                    $order->{addtlinfo}
    );
}

print "\n";

Last edited: 2021-10-07 09:58:21 +0200 (CEST)

View full thread Formatierte Ausgabe / Platzhalter ... aber wie ?