Exactly - $prefix contains the path taken so far, that's what the recursion does.

Again, why are you trying to print out the stuff from within find_joins? Please, don't do that. Just don't. Take the results of find_joins and then, convert them to the output format. It's much easier that way instead of trying to make a recursive function output the stuff you want at the end.

Your way of iterating over arrays is cumbersome, as you never need the array index anywhere:

for my $i ( 0 .. $#results ) {

is easier written as

for my $element ( @results ) {

But other than that, your code is close once you just take the data structure returned by find_joins and work on that:

# Build a lookup table for "left_table;right_table" => [ join columns +] my %link_columns = map { $_->[0] . ";" . $_->[1] => [ $_->[2], $_->[3] ], $_->[1] . ";" . $_->[0] => [ $_->[3], $_->[2] ] } @links; my @results = find_joins( 'apple' => 'owl' ); die "No path from 'apple' to 'owl'" unless @results; my $shortest_path = $results[0]; my $start = shift @$shortest_path; print "SELECT * FROM "; print "$start\n"; my $left = $start; for my $table (@$shortest_path) { my $key = "$left;$table"; print "-- $key\n"; my $join_cols = $link_columns{$key}; print " inner join $table on $join_cols->[0] = $join_cols->[1]\n"; $left = $table; };

In reply to Re^3: Advise on sql table mapping. by Corion
in thread Advise on sql table mapping. by dbmathis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.