#!/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 "
This is reading from array and printing from hash
";        dump_hash_categories($categories);        foreach (sort keys %category) { print "$category{$_}
";    }     #-############################################# # Alternative: # Read from array # and print a list of all categories ... #-#############################################    print "
This is reading and printing from Array
";    my @category = dump_categories($categories);    print "$_
" 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;