in reply to Sort mechanics problems with objects and potentially contradicting comparisons (would cause infinite loop)

One of the table declarations I configure takes the form: "FK", local column, foreign table name, foreign PK. So I have to sort the CSVs so that wherever such a declaration exists, the foreign table is created before the table in which the FK declaration is made.

Why not use a Dependency graph?

use Graph; my $g = Graph->new(directed => 1); $g->add_edge(split /:/) for qw/ a:d a:b c:b d:c /; my @order = $g->topological_sort; print join(", ", @order), "\n"; __END__ a, d, c, b

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Sort mechanics problems ...
by anonymized user 468275 (Curate) on Jun 02, 2016 at 11:18 UTC
    You are indeed correct that the classic solution to the problem is a graph and the type of sort is topological. However, the perl code is not compelled to support every step of the theory. It turns out to be much simpler in practice with perl sort:
    if $a is dependent on $b, return -1 if the reverse, return 1 otherwise return 0
    this lets $a and $b remain unsorted relative to each other where there is no dependency.

    One world, one people

      ...But, as you've discovered, it's not actually that simple. Another issue is that you'll need a stable sort algorithm for this to work (see the sort pragma). We're trying to tell you that sort is really not the right tool for this job.
Re^2: Sort mechanics problems ...
by anonymized user 468275 (Curate) on Jun 15, 2016 at 13:17 UTC
    This was definitely the most helpful answer. Thanks for taking the trouble!

    One world, one people