#!perl buildSQL($starttable, $startwhere) . . . . . . sub buildSQL { my $table=shift; my $where=shift; $level++; $ischild{$table}=1; foreach my $child (keys($tables{$table})) { print STDERR "About to gen SQL for $child, child of $table\n" if $table =~ m/Test2/; buildSQL($child,$wh); } buildParentSQL($table); $level--; } sub buildParentSQL () { my $table=shift; foreach my $parent (@{$parents{$table}}) { next if defined($ischild{$parent}; print STDERR "Generating parent SQL for $parent, parent of $table\n" if $parent =~ m/Test2/; buildParentSQL($parent); $level--; } }