Schrift
[thread]6200[/thread]

Ein Katalog-System: Titel und Untertitel (Seite 2)

Leser: 1


<< |< 1 2 >| >> 16 Einträge, 2 Seiten
Gast Gast
 2004-04-25 17:26
#81670 #81670
Edit: schnell mal den verkorksten Code gelöscht ;-)\n\n

<!--EDIT|Dieter|1083055056-->
Crian
 2004-04-26 14:08
#81671 #81671
User since
2003-08-04
5866 Artikel
ModeratorIn
[Homepage]
user image
Einrückungen wären hilfreich (Tabs in Blanks wandeln ev. auch)
s--Pevna-;s.([a-z]).chr((ord($1)-84)%26+97).gee; s^([A-Z])^chr((ord($1)-52)%26+65)^gee;print;

use strict; use warnings; Link zu meiner Perlseite
Gast Gast
 2004-04-26 17:43
#81672 #81672
Heureka - es läuft ... :D
War (natürlich) alles eingerückt - allerdings mit tabs ;)

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/perl
#-#############################################
# catalog.pl
# Version: 1.05
# Date: 04/26/2004
#-#############################################
# Copyright (C) 2002 Dieter Werner
# http://www.interwer.com
# [EMAIL=hdw@interwer.com]hdw@interwer.com[/EMAIL]
#-#############################################
print "Content-type: text/html\n\n";
use strict;
use warnings;

#-#############################################

my $categories = [
   ['Telekommunikation',
       ['Festnetz',
           ['Telefon',
               ['Foo', 'Bar'],
           ],
           ['Telefax',
               ['Foo', 'Bar'],
           ],
       ],
       ['Mobilnetz',
           ['Handy used',
               ['Siemens',
                   ['C25', 'C35', 'Other'],
               ],
               ['Nokia',
                   ['5130', '7710', '8810', 'Other'],
               ],
               ['Panasonic',
                   ['GD90', 'GD91', 'GD92'],
               ],
           ],
           ['Handy unused',
               ['Siemens',
                   ['C25', 'C35', 'Other'],
               ],
               ['Nokia',
                   ['5130', '7710', '8810', 'Other'],
               ],
               ['Panasonic',
                   ['GD90', 'GD91', 'GD92'],
               ],
           ],
       ],
   ],
   
   ['Computer',
       ['Hardware',
           ['Monitor',
               ['Eizo',
                   ['15 Zoll', '17 Zoll', '19 Zoll', '20 Zoll'],
               ],
               ['Other',
                   ['15 Zoll', '17 Zoll', '19 Zoll', '20 Zoll'],
               ],
           ],
           ['CPU',
               ['Intel',
                   ['up to 300 MHz', 'up to 600 MHz', 'up to 900 MHz'],
               ],
               ['AMD',
                   ['up to 300 MHz', 'up to 600 MHz', 'up to 900 MHz'],
               ],
               ['Other',
                   ['up to 300 MHz', 'up to 600 MHz', 'up to 900 MHz'],
               ],
           ],
       ],
       ['Software',
           ['Operating Systems',
               ['Windows', 'Linux'],
           ],
           ['Applications',
               ['Office-Software', 'Internet-Software', 'Games'],
           ],
       ],
   ],
];

#-#############################################
# Test:
# Convert array to hash
# and print the list of categories ...
#-#############################################
   my $catalog = {};
   
   dump_cat($_, $categories->[$_], $catalog) for 0 .. 1;
   print "$_ => $catalog->{$_}<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 $_;
       
       foreach (0 .. $#$cat) {
           ref $cat->[$_]
               ?   dump_sub_cat($cat->[$_], $catalog, \@index)
               :   do {
                       @index = ($cnt, $_);
                       $catalog->{join('_', @index)} = $cat->[$_];
                   };
       }
}

#-#############################################
# Dump Sub-Categories
#-#############################################
sub dump_sub_cat {
   my ($cat, $catalog, $index) = @_;
   local $_;

   my $dump = sub {
       my ($dump, $cat, $catalog, $index) = @_;
       my $cnt = $#$index;
       my $last = $index->[-1];
       local $_;

       $last++;
       
           foreach (0 .. $#$cat) {
               ref $cat->[$_]
                   ?   do {
                           $dump->($dump, $cat->[$_], $catalog, $index);
                           pop @$index;
                       }
                   :   do {
                           $_ == 0
                               ?   (@$index = (@$index[0 .. ($cnt - 1)], $last, $_))
                               :   (@$index = (@$index[0 .. $cnt], $_));
                           
                           $catalog->{join('_', @$index)} = $cat->[$_];
                       };
           }
   };
   
       foreach (0 .. $#$cat) {
           ref $cat->[$_]
               ?   do {
                       $dump->($dump, $cat->[$_], $catalog, $index);
                       pop @$index;
                   }
               :   do {
                       $_ == 0
                           ?   (@$index = ($index->[0], ++$index->[1], $_))
                           :   (@$index = (@$index[0 .. 1], $_));
                       
                       $catalog->{join('_', @$index)} = $cat->[$_];
                   };
       }
}

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


Verbesserungsvorschläge?
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;
Gast Gast
 2004-04-30 20:57
#81674 #81674
Parents und Childs sind nun eingearbeitet.
Der daraus resultierende Hash hat somit den Aufbau:

Array-Index => [
  'Text-Titel des Knotens',
  'Parent-Index des Knotens,
  'Child(s) des Knotens'
],

Werde wohl in den nächsten Tagen mal ein Modul daraus machen.

Kommentare und Verbesserungsvorschläge zum Code sind sehr willkommen :)

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/perl
#-#############################################
# catalog.pl
# Version: 1.08
# Date: 04/30/2004
#-#############################################
# LICENSE-CONDITIONS:
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#-#############################################
#
# In accordance with the GNU General Public License (GPL):
# - You may not use and/or distribute this code under a proprietary license!
# - You either may not distribute parts of this code under a proprietary license too!
# - The following copyright notice and the further following copyright notices of this
#   software, MUST remain UNCHANGED and INTACT!
#
#-#############################################
#
# Copyright (2004) by Dieter Werner
# http://www.interwer.com
# eMail: [EMAIL=hdw@interwer.com]hdw@interwer.com[/EMAIL]
# All rights reserved by the author.
#
#-#############################################
# OK - here we go ...

#-#############################################
# Use-Section
#-#############################################
   use strict;
   use warnings;
   
#-#############################################
# Setup-Section
#-#############################################
   print "Content-type: text/html\n\n";
   $| = 1;
   my $catalog = {};

#-#############################################
# The Categories
# This will be the content of a text file.
#-#############################################
   my $categories = <<EO_CAT;
       ['Computer',
           ['Hardware',
               ['Monitor',
                   ['Eizo',
                       [
                           '15 Zoll',
                           '17 Zoll',
                           '19 Zoll',
                           '20 Zoll',
                       ],
                   ],
                   ['Other',
                       [
                           '15 Zoll',
                           '17 Zoll',
                           '19 Zoll',
                           '20 Zoll',
                       ],
                   ],
               ],
               ['CPU',
                   ['Intel',
                       [
                           'up to 300 MHz',
                           'up to 600 MHz',
                           'up to 900 MHz',
                       ],
                   ],
                   ['AMD',
                       [
                           'up to 300 MHz',
                           'up to 600 MHz',
                           'up to 900 MHz',
                       ],
                   ],
                   ['Other',
                       [
                           'up to 300 MHz',
                           'up to 600 MHz',
                           'up to 900 MHz',
                       ],
                   ],
               ],
           ],
           ['Software',
               ['Operating Systems',
                   [
                       'Windows',
                       'Linux',
                   ],
               ],
               ['Applications',
                   [
                       'Office-Software',
                       'Internet-Software',
                       'Games',
                   ],
               ],
           ],
       ],
       ['Electronics',
           ['Audio',
               ['Player',
                   [
                       'Akai',
                       'Sony',
                       'Panasonic',
                       'Other',
                   ],
               ],
               ['Recorder',
                   [
                       'Akai',
                       'Sony',
                       'Panasonic',
                       'Other',
                   ],
               ],
               ['HiFi',
                   [
                       'Akai',
                       'Sony',
                       'Panasonic',
                       'Other',
                   ],
               ],
           ],
           ['Video',
               ['Recorder',
                   ['Akai',
                       ['C25',
                           [
                               'Black',
                               'White',
                               'Green',
                           ],
                       ],
                       ['C35',
                           [
                               'Red',
                               'Blue',
                               'Yellow',
                           ],
                       ],
                       ['Other',
                           [
                               'Black',
                               'White',
                               'Green',
                           ],                        
                       ],
                   ],
                   ['Sony',
                       [
                           '5130',
                           '7710',
                           '8810',
                           'Other',
                       ],
                   ],
                   ['Panasonic',
                       [
                           'GD90',
                           'GD91',
                           'GD92',
                       ],
                   ],
               ],
               ['Player',
                   ['Akai',
                       [
                           'C25',
                           'C35',
                           'Other',
                       ],
                   ],
                   ['Sony',
                       [
                           '5130',
                           '7710',
                           '8810',
                           'Other',
                       ],
                   ],
                   ['Panasonic',
                       [
                           'GD90',
                           'GD91',
                           'GD92',
                       ],
                   ],
               ],
           ],
       ],
EO_CAT
   
#-#############################################
# Only for Testing the Subroutine.
# Convert array to hash
# and print the list of categories ...
#-#############################################
   $categories = do 'categories.ini' || [eval $categories]; # Hi Randal;-))
   
   get_index(
       $_,
       $categories->[$_],
       $catalog
   ) foreach (0 .. $#$categories);    

   # Print it out

       foreach (sort keys %$catalog) {
           print $_, ' => ', $catalog->{$_}->[0];
           print '<br>Parent: ', $catalog->{$_}->[1] if $catalog->{$_}->[1];
           print '<br>';
           
           $catalog->{$_}->[2] && do {
               my @childs = @{$catalog->{$_}}[2 .. $#{$catalog->{$_}}];
               my $childs = join ' | ', @childs;
               print 'Childs: ', $childs, '<hr>';
           };
       }

#-#############################################
sub get_index {
#-#############################################
   my ($cnt, $cat, $catalog) = @_;
   my (@sub_cat, @parent, @index);
   local $_;
       
   # Dump Sub-Categories
   # Get index, parent, child(ren) of each Sub-Category
   
   my $dump = sub {
       my (
           $dump,
           $cat,
           $sub_cat,
           $catalog,
           $index,
           $parent
       ) = @_;
       
       my $cnt = $#$index;
       my $prev_last = $index->[-1];
       local $_;
       
       ($cat->[0] and !ref $cat->[0]) && push @$sub_cat, $cat->[0];
       
           foreach (0 .. $#$cat) {
               ref $cat->[$_]
                   ?   do {                            
                           $dump->(
                               $dump,
                               $cat->[$_],
                               $sub_cat,
                               $catalog,
                               $index,
                               $parent,
                           );
                           
                           pop @$sub_cat;
                           pop @$index;
                       }
                   :   do {
                           $_ == 0
                               ?   do {
                                       @$parent = (
                                           @$index[0 .. ($cnt - 1)],
                                           0
                                       );
                                       
                                       $#$index < 2
                                           ?   (
                                                   @$index = (
                                                       $index->[0],
                                                       ++$index->[1],
                                                       $_
                                                   )
                                               )
                                           :   do {
                                                   $prev_last++;
                                                   @$index = (
                                                       @$index[0 .. ($cnt - 1)],
                                                       $prev_last, $_
                                                   );
                                               };
                                   }
                               :   do {
                                       @$parent = (@$parent[0 .. ($cnt - 1)], 0);
                                       
                                       $#$index < 2
                                           ?   (@$index = (@$index[0 .. 1], $_))
                                           :   (@$index = (@$index[0 .. $cnt], $_));
                                   };
                           
                           my $index = join '_', @$index;
                           my $parent = join  '_', @$parent;
                           
                           my $the_cat = join "/", (
                               @$sub_cat[0 .. ($#$sub_cat - 1)],
                               $cat->[$_]
                           );

                           # Add the Title and the Parent
                           $catalog->{$index} = [
                               $the_cat,
                               $parent
                           ];

                           # Add the found Child
                           push @{$catalog->{$parent}}, $index;
                       };
           }
   };
   
   # Dump Main-Categories
   # Get the index of each Main-Category      

       foreach (0 .. $#$cat) {
           ref $cat->[$_]
               ?   do {                        
                       $dump->(
                           $dump,
                           $cat->[$_],
                           \@sub_cat,
                           $catalog,
                           \@index,
                           \@parent,
                       );
                       
                       pop @sub_cat,
                       pop @index;
                   }
               :   do {
                       push @sub_cat, $cat->[$_];
                       
                       @index = ($cnt, $_);
                       @parent = @index;

                       $catalog->{join('_', @index)} = [
                           join("/", (@sub_cat[0 .. ($#sub_cat - 1)], $cat->[$_])),
                           undef
                       ];
                   };
       }
}

#-#############################################
exit;
\n\n

<!--EDIT|Dieter|1083424700-->
Gast Gast
 2004-05-01 16:47
#81675 #81675
Ein wenig konstruktive Kritik würde mir tatsächlich weiterhelfen :)\n\n

<!--EDIT|Dieter|1085323237-->
<< |< 1 2 >| >> 16 Einträge, 2 Seiten



View all threads created 2004-04-18 16:40.