Thread Ein Katalog-System: Titel und Untertitel (15 answers)
Opened by Gast at 2004-04-18 16:40

Gast Gast
 2004-04-27 18:08
#81673 #81673
Hab das Teil ein wenig gekürzt und für die Ermittlung von Parent/Child vorbereitet.

Code: (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
#-#############################################
# Test:
# Convert array to hash
# and print the list of categories ...
#-#############################################    
   dump_cat($_, $categories->[$_], $catalog) for 0 .. 1;
   print "$_ => $catalog->{$_}->[0]<br>" foreach sort keys %$catalog;
   print "<hr>";
   disp_cat($catalog);
   
#-#############################################
# Test:
# Display Catalog
#-#############################################
sub disp_cat {
   my $catalog = shift;
   my $key;
   
       foreach $key (sort keys %$catalog) {
           my $index = '$index = $categories->[';
           $index .= join '][', split /_/, $key;
           $index .= ']';
           eval $index;
           print $index, " => ",  $key, "<br>";
       }
}

#-#############################################
# Dump Main-Categories
#-#############################################
sub dump_cat {
   my ($cnt, $cat, $catalog) = @_;
   my @index;
   local $_;
   
   # The recursion
   my $dump = sub {
       my ($dump, $cat, $catalog, $index) = @_;
       my $cnt = $#$index;
       my $last = $index->[-1];
       local $_;
       
           foreach (0 .. $#$cat) {
               ref $cat->[$_]
                   ?   do {
                           $dump->(
                               $dump,
                               $cat->[$_],
                               $catalog,
                               $index
                           );
                           
                           pop @$index;
                       }
                   :   do {
                           $_ == 0
                               ?   do {
                                       $#$index < 2
                                           ?   do {
                                                   @$index = ($index->[0], ++$index->[1], $_);
                                                   # Placeholder Parent
                                                   # Placeholder Child(s)
                                               }
                                           :   do {
                                                   $last++;
                                                   @$index = (@$index[0 .. ($cnt - 1)], $last, $_);
                                                   # Placeholder Parent
                                                   # Placeholder Child(s)
                                               };
                                               
                                   }
                               :   do {
                                       $#$index < 2
                                           ?   do {
                                                   @$index = (@$index[0 .. 1], $_);
                                                   # Placeholder Parent
                                                   # Placeholder Child(s)
                                           }
                                           :   do {
                                                   @$index = (@$index[0 .. $cnt], $_);
                                                   # Placeholder Parent
                                                   # Placeholder Child(s)
                                               };
                                   };
                       
                           $catalog->{join('_', @$index)} = [
                               $cat->[$_],
                               undef, # Placeholder Parent
                               undef, # Placeholder Child(s)
                           ];
                       };
           }
   };
       
       foreach (0 .. $#$cat) {
           ref $cat->[$_]
               ?   do {
                       $dump->(
                           $dump,
                           $cat->[$_],
                           $catalog, \@index
                       );
                       
                       pop @index;
                   }
               :   do {
                       @index = ($cnt, $_);
                       $catalog->{join('_', @index)} = [
                           $cat->[$_],
                           0, # Has no Parent
                           undef, # Placeholder Child(s)
                       ];
                   };
       }
}

#-#############################################
exit;

View full thread Ein Katalog-System: Titel und Untertitel