Schrift
[thread]6200[/thread]

Ein Katalog-System: Titel und Untertitel

Leser: 1


<< |< 1 2 >| >> 16 Einträge, 2 Seiten
Gast Gast
 2004-04-18 16:40
#81660 #81660
An dem Teil breche ich mir nun schon seit Jahren den Arm und krieg das nicht gebacken.
Kram das Ding immer mal wieder raus - aber so richtig komme ich damit nicht weiter.

Es soll ein Inhaltsverzeichnis/Katalogsystem sein (Haupttitel, Untertitel, Unter-Untertitel, usw.) also irgendwie sowas wie eine Verzeichnisstruktur.

Der nachfolgende Code macht (hinsichtlich der Anzeige) auch genau das was ich davon erwarte ...

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
#!/usr/bin/perl
#-#############################################
# catalog.pl
# Version: 1.03
# Date: 08/09/2002
#-#############################################
   print "Content-type: text/html\n\n";
   $|++;
   use strict;
   use warnings;
   our %category;    
#-#############################################

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',
['Panasonic',
   ['GD90', 'GD91', 'GD92']
],
['Siemens',
   ['C25', 'C35', 'Other']
],
['Nokia',
   ['5130', '7710', '8810', 'Other']
]
   ]
]
   ],
   
   ['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']
   ]
]
   ]
];

#-#############################################
# Convert array to hash
# and print a list of categories ...
#-#############################################
   print "<hr>This is reading from array and printing from hash<hr>";
   
   dump_hash_categories($categories);
   
   foreach (sort keys %category) {
print "$category{$_}<br>";
   }
   
#-#############################################
# Alternative:
# Read from array
# and print a list of all categories ...
#-#############################################
   print "<hr>This is reading and printing from Array<hr>";

   my @category = dump_categories($categories);
   print "$_ <br>" for @category;
 
#-#############################################
# Dump Main-Categories
#-#############################################
sub dump_categories {
   my $catalog = shift;
   my ($category, $main_cat, $show_cat);
       
while ($#$catalog >= 0) {
   $category = shift @$catalog;
      while ($#$category >= 0) {
   $main_cat = shift @$category;      
if (ref $main_cat) {
   dump_subcat($main_cat, $show_cat, \@category);
}
else {
   $show_cat = $main_cat;
   push @category, $show_cat;
}
      }
}
   
   sort @category;
}

#-#############################################
# Dump Sub-Categories
#-#############################################
sub dump_subcat {
   my ($sub_main, $show_cat, $catalog) = @_;
   my $sub_cat;
       
while ($#$sub_main >= 0) {
   $sub_cat = shift @$sub_main;
if (ref $sub_cat) {
   dump_subcat($sub_cat, $show_cat, $catalog);
}
else {
   $show_cat .= " >> $sub_cat";
   push @$catalog, $show_cat;
   $show_cat =~ s/(\>\>\s$sub_cat)$// unless ref $sub_main->[0];
}
}
}
 
#-#############################################
# Dump Categories as a hash
#-#############################################
sub dump_hash_categories {
   my ($cat, @sub_cat) = @_;
   
   push @sub_cat, $cat->[0] if $cat->[0] and !ref $cat->[0];
   
foreach my $sub_cat (@$cat) {
   if (ref $sub_cat) {
dump_hash_categories($sub_cat, @sub_cat);
   }
   else {
$category{join("_", @sub_cat[0..($#sub_cat - 1)], $sub_cat)}
= join(" >> ", @sub_cat[0..($#sub_cat - 1)], $sub_cat);
   }
}
}

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


Hat jemand eine Idee wie ich nun einen Hash bilden kann der zu jedem (Klartext) Array-Element den entsprechen Array-Index enthält?
z.B.  'Eizo' => '102113',
(die Zahlenfolge hab ich hier einfach mal willkürlich eingetragen)

Arrrgh ...
die tabs werden vom Forum nicht übernommen :(
renee
 2004-04-19 10:21
#81661 #81661
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Mit diesem Code:
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
#! /usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

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',
['Panasonic',
['GD90', 'GD91', 'GD92']
],
['Siemens',
['C25', 'C35', 'Other']
],
['Nokia',
['5130', '7710', '8810', 'Other']
]
]
]
],
];

my $hashref = getPosition($categories,"","");

print Dumper($hashref);

sub getPosition{
my ($arrayref,$title,$position) = @_;
my @array = @{$arrayref};
my %hash = ();
my $ref2hash = ();
foreach my $index(0..(scalar(@array) - 1)){
my $type = ref($array[$index]);
if($type eq 'ARRAY'){
$position .= '_'.$index;
$ref2hash = getPosition($array[$index],$title,$position);
%hash = (%hash,%{$ref2hash});
}# elsif
unless($type){
$title = $title.'_'.$array[$index];
my $pos = $position.'_'.$index;
$title =~ s/^_//;
$pos =~ s/^_//;
$hash{$title} = $pos;
}# if
}# foreach
return \%hash;
}# end getPosition
bekomme ich das hier raus:
Quote
$VAR1 = {
'Telekommunikation_Mobilnetz_Handy unused_Siemens_C25_C35_Other' => '0_1_2_1_2_1_2_1_2',
'Telekommunikation_Mobilnetz_Handy unused' => '0_1_2_1_2_0',
'Telekommunikation_Mobilnetz_Handy unused_Siemens_C25_C35' => '0_1_2_1_2_1_2_1_1',
'Telekommunikation_Mobilnetz_Handy used_Siemens' => '0_1_2_1_1_0',
'Telekommunikation_Festnetz_Telefax_Foo' => '0_1_1_2_1_0',
'Telekommunikation_Mobilnetz_Handy unused_Nokia' => '0_1_2_1_2_1_2_3_0',
'Telekommunikation' => '0_0',
'Telekommunikation_Mobilnetz_Handy used' => '0_1_2_1_0',
'Telekommunikation_Mobilnetz' => '0_1_2_0',
'Telekommunikation_Mobilnetz_Handy unused_Nokia_5130_7710_8810' => '0_1_2_1_2_1_2_3_1_2',
'Telekommunikation_Mobilnetz_Handy used_Nokia_5130_7710_8810_Other' => '0_1_2_1_1_2_1_3',
'Telekommunikation_Mobilnetz_Handy unused_Nokia_5130_7710' => '0_1_2_1_2_1_2_3_1_1',
'Telekommunikation_Festnetz_Telefax' => '0_1_1_2_0',
'Telekommunikation_Festnetz_Telefax_Foo_Bar' => '0_1_1_2_1_1',
'Telekommunikation_Mobilnetz_Handy used_Nokia' => '0_1_2_1_1_2_0',
'Telekommunikation_Mobilnetz_Handy unused_Panasonic_GD90_GD91_GD92' => '0_1_2_1_2_1_1_2',
'Telekommunikation_Mobilnetz_Handy used_Panasonic_GD90' => '0_1_2_1_1_2_3_1_0',
'Telekommunikation_Mobilnetz_Handy unused_Panasonic_GD90_GD91' => '0_1_2_1_2_1_1_1',
'Telekommunikation_Mobilnetz_Handy unused_Siemens_C25' => '0_1_2_1_2_1_2_1_0',
'Telekommunikation_Mobilnetz_Handy used_Siemens_C25_C35' => '0_1_2_1_1_1_1',
'Telekommunikation_Festnetz_Telefon_Foo_Bar' => '0_1_1_1_1',
'Telekommunikation_Mobilnetz_Handy used_Panasonic_GD90_GD91' => '0_1_2_1_1_2_3_1_1',
'Telekommunikation_Mobilnetz_Handy unused_Panasonic_GD90' => '0_1_2_1_2_1_1_0',
'Telekommunikation_Mobilnetz_Handy used_Nokia_5130_7710_8810' => '0_1_2_1_1_2_1_2',
'Telekommunikation_Mobilnetz_Handy used_Nokia_5130_7710' => '0_1_2_1_1_2_1_1',
'Telekommunikation_Mobilnetz_Handy used_Panasonic' => '0_1_2_1_1_2_3_0',
'Telekommunikation_Festnetz' => '0_1_0',
'Telekommunikation_Mobilnetz_Handy unused_Nokia_5130' => '0_1_2_1_2_1_2_3_1_0',
'Telekommunikation_Mobilnetz_Handy used_Siemens_C25_C35_Other' => '0_1_2_1_1_1_2',
'Telekommunikation_Festnetz_Telefon' => '0_1_1_0',
'Telekommunikation_Mobilnetz_Handy used_Nokia_5130' => '0_1_2_1_1_2_1_0',
'Telekommunikation_Mobilnetz_Handy used_Siemens_C25' => '0_1_2_1_1_1_0',
'Telekommunikation_Mobilnetz_Handy unused_Siemens' => '0_1_2_1_2_1_2_0',
'Telekommunikation_Mobilnetz_Handy unused_Nokia_5130_7710_8810_Other' => '0_1_2_1_2_1_2_3_1_3',
'Telekommunikation_Mobilnetz_Handy used_Panasonic_GD90_GD91_GD92' => '0_1_2_1_1_2_3_1_2',
'Telekommunikation_Festnetz_Telefon_Foo' => '0_1_1_1_0',
'Telekommunikation_Mobilnetz_Handy unused_Panasonic' => '0_1_2_1_2_1_0'
};


Die einzelnen Ebenen sind mit _ getrennt. Eine Position 3_5 sagt also aus, dass das gesuchte Teil im dritten Element der ersten Ebene und im fünften Element der 2. Ebene liegt...
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
Gast Gast
 2004-04-19 15:22
#81662 #81662
Danke für den Code renee.
Klappt aber leider nicht wenn ich mit der von der Routine erzeugten Indizierung auf das Katalog-Array zugreifen will.
renee
 2004-04-19 16:01
#81663 #81663
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Ich verstehe jetzt nicht so ganz, wo da der große Unterschied zu Deinem gewünschten Format ist. Außer, dass man bei meiner Methode auch mehr als 9 Menüpunkte haben kann. Bei einer durchgehenden Zahl weiß man nämlich nicht, ob es eine 10 oder eben eine 1 und eine 0 ist. Du kannst ja die zusammengesetzte Zahl durch split /_/,$string wieder trennen...
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
Gast Gast
 2004-04-19 17:38
#81664 #81664
Quote
Ich verstehe jetzt nicht so ganz, wo da der große Unterschied zu Deinem gewünschten Format ist.


So hab ich das nicht gemeint - das Format ist absolut OK.
Aber schau mal:
Telekommunikation_Festnetz_Telefax_Foo_Bar
hat in $VAR den Index: 0_1_1_2_1_1'
und die tatsächliche Array-Position ist: 0_1_1_1_1

und da liegt denn auch das Problem bei dem ich ständig gegen die Wand renne - ich krieg es einfach nicht hin die Indizes richtig zusammenzubacken.
renee
 2004-04-19 17:56
#81665 #81665
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Ich habe den Code der Sub mal verbessert und getestet.
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
sub getPosition{
 my ($arrayref,$title,$position) = @_;
 my @array = @{$arrayref};
 my %hash = ();
 my $ref2hash = ();
 foreach my $index(0..(scalar(@array) - 1)){
   my $type = ref($array[$index]);
   if($type eq 'ARRAY'){
     $position .= '_'.$index;
     $ref2hash = getPosition($array[$index],$title,$position);
     %hash = (%hash,%{$ref2hash});
   }# elsif
   unless($type){
     my $s = $array[$index];
     my $key = $title;
     $key = $key.'_'.$s;
     $key =~ s/\_[^\_]+$/\_$s/ if($index != 0);
     $title = $title.'_'.$s if((ref($array[$index + 1]) eq 'ARRAY') && $index == 0);
     $title =~ s/\_[^\_]+$/\_$s/ if((ref($array[$index + 1]) eq 'ARRAY') && $index != 0);
     my $pos = $position.'_'.$index;
     $title =~ s/^_//;
     $key =~ s/^_//;
     $pos =~ s/^_//;
     $hash{$key} = $pos;
   }# if
 }# foreach
 return \%hash;
}# end getPosition

Die Positionen stimmen eigentlich Vergleiche die Position von
Quote
'Telekommunikation_Festnetz_Telefon_Foo' => '0_1_1_1_0',
und die Ausgabe von
Quote
print $categories->[0]->[1]->[1]->[1]->[0];
\n\n

<!--EDIT|renee|1082383000-->
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
Gast Gast
 2004-04-19 22:20
#81666 #81666
läuft leider immer noch nicht ...
aber ich glaube ich bin dank Deiner Hilfe jetzt nahe dran ...
renee
 2004-04-20 00:51
#81667 #81667
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Wäre super, wenn Du - falls Du die Lösung hast - diese hier posten könntest. Würde mich dann auch interessieren!
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
Gast Gast
 2004-04-20 17:23
#81668 #81668
Keine Frage renee - das kommt dann ... :)
Gast Gast
 2004-04-24 21:13
#81669 #81669
Also ich hab das jetzt mal ganz 'flach' aufgebaut (linear, keine Rekursion verwendet) und kann damit dann tatsächlich den Index für jedes Array-Element ermitteln.
Der nachfolgende Code ist natürlich mehr als unbefriediegend - aber immerhin ein Ansatz für nachfolgende Optimierung.
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
#!/usr/bin/perl
#-#############################################
# catalog.pl
# Version: 1.04
# Date: 04/25/2004
# Written by Dieter Werner
#-#############################################
   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'],
    ],
],
   ],
];
   
#-#############################################
# Read from array
# and print a list of all categories => Indices
#-#############################################
my $catalog = {};

   dump_cat_1($_, $categories->[$_], $catalog) for 0 .. 1;
   print "$_ => $catalog->{$_}<br>" foreach sort keys %$catalog;
 
#-#############################################
# Dump Level 1
#-#############################################
sub dump_cat_1 {
   my ($cnt, $cat, $catalog) = @_;
my @index;
local $_;
       
for (0 .. $#$cat) {
ref $cat->[$_]
? dump_cat_2($cat->[$_], $catalog, \@index)
: do {
@index = ($cnt, $_);
$catalog->{join('_', @index)} = $cat->[$_];
}
}
}

#-#############################################
# Dump Level 2
#-#############################################
sub dump_cat_2 {
   my ($cat, $catalog, $index) = @_;
local $_;

for (0 .. $#$cat) {
ref $cat->[$_]
? dump_cat_3($cat->[$_], $catalog, $index)
: do {
$_ == 0
? (@$index = ($index->[0], ++$index->[1], $_))
: (@$index = (@$index[0 .. 1], $_));

$catalog->{join('_', @$index)} = $cat->[$_];
}
}
}

#-#############################################
# Dump Level 3
#-#############################################
sub dump_cat_3 {
   my ($cat, $catalog, $index) = @_;
local $_;

for (0 .. $#$cat) {
ref $cat->[$_]
? dump_cat_4($cat->[$_], $catalog, $index)
: do {
$_ == 0
? (@$index = (@$index[0 .. 1], ++$index->[2], $_))
: (@$index = (@$index[0 .. 2], $_));

$catalog->{join('_', @$index)} = $cat->[$_];
}
}
}
 
#-#############################################
# Dump Level 4
#-#############################################
sub dump_cat_4 {
   my ($cat, $catalog, $index) = @_;
local $_;

for (0 .. $#$cat) {
ref $cat->[$_]
? dump_cat_5($cat->[$_], $catalog, $index)
: do {
$_ == 0
? (@$index = (@$index[0 .. 2], ++$index->[3], $_))
: (@$index = (@$index[0 .. 3], $_));

$catalog->{join('_', @$index)} = $cat->[$_];
}
}
}
 
#-#############################################
# Dump Level 5
#-#############################################
sub dump_cat_5 {
   my ($cat, $catalog, $index) = @_;
local $_;

for (0 .. $#$cat) {
ref $cat->[$_]
? dump_cat_6($cat->[$_], $catalog, $index)
: do {
$_ == 0
? (@$index = (@$index[0 .. 3], ++$index->[4], $_))
: (@$index = (@$index[0 .. 4], $_));

$catalog->{join('_', @$index)} = $cat->[$_];
}
}
}

#-#############################################
# Dump Level 6
#-#############################################
sub dump_cat_6 {
   my ($cat, $catalog, $index) = @_;
local $_;

for (0 .. $#$cat) {
ref $cat->[$_]
? dump_cat_7($cat->[$_], $catalog, $index)
: do {
$_ == 0
? (@$index = (@$index[0 .. 4], ++$index->[5], $_))
: (@$index = (@$index[0 .. 5], $_));

$catalog->{join('_', @$index)} = $cat->[$_];
}
}
}

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


Ächz - warum übernimmt das Forum meine Formatierung nicht??

Edit: Code verbessert und eingekürzt.\n\n

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



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