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

The syntax for custom sort routines is sometimes tricky to get right - towards the end of the sort page there are some examples of the syntax. It sounds like your problem might be fixed with the syntax sort {$obj->_DBsort4create(...args...)} keys %$csvQ, but I'm not sure as you haven't provided runnable example code...

As for your second question, I haven't yet wrapped my head around the potential problem you're describing (no sample code that demonstrates the infinite loop?), but if you're asking about optimizing the sort, then maybe a Schwartzian transform will help?

Regards,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Sort mechanics problems with objects and potentially contradicting comparisons (would cause infinite loop)
by anonymized user 468275 (Curate) on Jun 01, 2016 at 17:08 UTC
    Yes I agree with the idea of enclosing the sort call in a block, especially given that Perl documents this approach and it appears all the mechanics are fine, except the potential for infinite loop needing to be caught.

    The problem with sample code that is already wrong is that it night have a different problem from the same theoretical weakness. So the issue of circular dependencies should be resolved in theory before writing any more code.

    The optimisation part is best done using an orcish manoeuvre than a schwartzian transform because it is a question of not doing the same processing twice although the sort algorithm will call the function many times with the same table appearing in $a or $b for successive iterations. Except that this time, apart from storing past processing results of individual elements to prevent reiterating them I need also to store past comparisons to check for circular dependencies.

    One world, one people

      The problem with sample code that is already wrong is that it night have a different problem from the same theoretical weakness. So the issue of circular dependencies should be resolved in theory before writing any more code.

      Even if we discuss theory, you still need to communicate the potential problem somehow. So I disagree that code isn't useful at this point - it should be possible to construct a piece of sample code that demonstrates the infinite loop issue that you are worried about, even if it takes one or two iterations.

      Regards,
      -- Hauke D

        Sometimes you have to be careful what you wish for. The following test code should produce an infinite loop (because there is no documented alternative):
        #!/usr/bin/perl use strict; use warnings; my $iteration = 0; print sort byStupid (qw(A B C)); sub byStupid { $iteration++; $iteration > 10000 and die "10000 is a lot to sort three things"; if ($b eq 'C' and $a eq 'A') { $b cmp $a; } else { $a cmp $b; } }
        Unfortunately perl sort is content to produce incorrect output in finite time. i.e. it chose to ignore the correct positioning of B and C in the list and produced CAB as output. The correct behaviour would be an infinite loop or a fatal error because the array in any order is an incorrectly returned result from function 'sort'.

        One world, one people