in reply to Database categories

According to your table, "Sharks" has the sub-categories "Small Critters" and "Sharks"(!). That doesn't make sense. Both should probably be subcategories of "Salt Water".

The typical representation in Perl of a tree like yours would be a hash of arrays. Keys are the category names and values hold the list of corresponding sub-categories. Leaving all database issues aside, I'll assume (a corrected version of) your table is presented as a text file. The following code builds the tree hash %tree from the data. The sub print_tree then prints an indented table of the categories.

my @names; my %tree; while ( <DATA> ) { chomp; my ( $id, $parent, $name) = (split ' ', $_, 5)[ 2, 3, 4]; $names[ $id] = $name; if ( $parent ) { push @{ $tree{ $names[ $parent]} }, $name; } } print_tree( 'Animals', \ %tree, 0); exit; use constant INDENT => ' '; sub print_tree { my ( $root, $tree, $level) = @_; print INDENT x $level, $root, "\n"; for ( @{ $tree->{ $root} } ) { print_tree( $_, $tree, $level + 1); } } __DATA__ Edit Delete 1 0 Animals Edit Delete 2 1 Mammals Edit Delete 3 1 Fish Edit Delete 4 2 Dog Edit Delete 5 2 Cat Edit Delete 6 3 Fresh Water Edit Delete 7 3 Salt Water Edit Delete 8 7 Sharks Edit Delete 9 7 Small Critters
Anno

Replies are listed 'Best First'.
Re^2: Database categories
by coldfingertips (Pilgrim) on Apr 03, 2007 at 22:19 UTC
    Thanks for the information but I can't figure out how to query the MySQL data in such a way I could process the information with your subroutines.