my $sth = $dbh->prepare("select tablename from pg_tables where schemaname=?"); $sth->execute($schema); my @tables; while (my @t = $sth->fetchrow_array) { push @tables, @t; } $sth->finish; #### my %children = ( 'a' => [ 'b', 'c' ], 'c' => [ 'x' ], 'b' => [ 'x' ], 'x' => [ 'y' ], 'y' => [ 'z' ], 'z' => [ ], ); sub children { @{$children{$_[0]} || []}; } my @unsorted = ( 'z', 'a', 'x', 'c', 'b', 'y' ); my @sorted = toposort(\&children, \@unsorted);