Ok, here is my first attempt to converting your output into SQL. I have not added the table aliases into the code yet, and I am sure you can do this with far less code so let me know what you think.

I think the function calling itself makes sense now. I assume you are doing this to populate prefix. I may be wrong though.

#!/usr/bin/perl -w use strict; my @links = map { [/^(\w+)\s+(\w+)\s+(\w+)\s+(\w+)/] } split /\n/, <<L +INKS; apple bug unid1 unid2 apple cat unid1 unid3 bug dog unid2 unid4 apple dog unid1 unid4 dog owl unid4 unid5 LINKS # All links work both ways: my %links; for (@links) { my ($left,$right) = @$_; # We store all links in arrays $links{$left} ||= []; $links{$right} ||= []; # Links work both ways push @{$links{$left}}, $right; push @{$links{$right}}, $left; } sub find_joins { my ($start, $goal, $prefix, $visited) = @_; $visited ||= {}; # our breadcrumb trail so we dont go in circles $prefix ||= []; # the path so far my $curr = $start; #warn "[@$prefix] $start => $goal"; my @results; # All the paths that lead from $start to $end $visited->{$start} = 1; for my $link (grep { ! $visited->{$_} } @{$links{$start}}) { if ($link eq $goal) { push @results, [@$prefix,$start,$goal]; for my $i ( 0 .. $#results ) { for my $ii ( 0 .. $#{$results[$i]} ) { if ( $ii == 0 ) { print "$results[$i][$ii]\n"; } else { my $keys; for ( @links ) { my ( $left, $right, $lid, $rid ) = @$_; if ( $left eq $results[$i][$ii - 1] && $ri +ght eq $results[$i][$ii] ) { $keys = "on $lid = $rid"; } } print "inner join $results[$i][$ii] $keys\n"; } } print "\n"; } } else { my %v = %$visited; push @results, find_joins($link,$goal,[@$prefix,$start],\% +v); } } # Sort by length, then asciibetically @results = sort { scalar @$a <=> scalar @$b or $a->[0] cmp $b->[0] + } @results; } find_joins( 'apple' => 'owl' );

apple inner join bug on unid1 = unid2 inner join dog on unid2 = unid4 inner join owl on unid4 = unid5 apple inner join dog on unid1 = unid4 inner join owl on unid4 = unid5
After all this is over, all that will really have mattered is how we treated each other.

In reply to Re^2: Advise on sql table mapping. by dbmathis
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.