So I decided to let the database handle any circular dependencies (it will fail to define one of the tables and I have to then roll back everything) but apart from that I have a working topological sort. Thanks to all who helped out.
my $csvQ = {}; for my $csv ($obj->_globcsv) { my $base = basename $csv; $base =~ s/\.txt$//; $base = lc($base); $obj->_csv2arr($csv); $csvQ->{$base}{table} = $obj->_getset('ar'); } $obj->_getset('csvQ', $csvQ); for my $table ( sort _DBsort4create keys %$csvQ ) { $obj->_getset('ar', $csvQ->{$table}{table}); $obj->_arr2db; # process the csv into the db } sub _DBsort4create { my $obj = shift; my $csvQ = $obj->_getset('csvQ'); for my $ab ($a, $b) { unless(exists($csvQ->{$ab}{ancestor})) { $csvQ->{$ab}{ancestor} = {}; ROWDBS4C: for (my $row=1; $row <= $#{$csvQ->{$ab}{table}}; $ro +w++) { # avoid first row, which is a header not csv data ($csvQ->{$ab}{table}[$row][0] eq '__COLUMNS__') and last ROWDBS4C; if ($csvQ->{$ab}{table}[$row][0] eq 'FK') { $csvQ->{$ab}{ancestor}{$csvQ->{$ab}{table}[$row][2]}=' +'; } } } } if (exists($csvQ->{$a}{ancestor}{$b})) { return 1; } elsif (exists($csvQ->{$b}{ancestor}{$a})) { return -1; } return 0; }
Sorts by ancestral relationship of FKs. Minor optimisation similar to orcish manoevre: only search a csv array for FK entries once.

One world, one people


In reply to Re: Sort mechanics problems with objects and potentially contradicting comparisons (would cause infinite loop) by anonymized user 468275
in thread Sort mechanics problems with objects and potentially contradicting comparisons (would cause infinite loop) by anonymized user 468275

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.