Leser: 1
![]() |
|< 1 2 >| | ![]() |
16 Einträge, 2 Seiten |
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;
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
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'
};
QuoteIch verstehe jetzt nicht so ganz, wo da der große Unterschied zu Deinem gewünschten Format ist.
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
Quoteund die Ausgabe von'Telekommunikation_Festnetz_Telefon_Foo' => '0_1_1_1_0',
Quote\n\nprint $categories->[0]->[1]->[1]->[1]->[0];
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;
![]() |
|< 1 2 >| | ![]() |
16 Einträge, 2 Seiten |