#-#############################################
# 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;