roboticus has asked for the wisdom of the Perl Monks concerning the following question:

Hello, fellow PerlMonks!

I joined back in March, but this is my first question to SoPW. A fellow monk suggested that I read Resorting to Sorting to get a handle on some alternate sorting idioms. While doing so, I'm having a spot of trouble with Exercise 2 in the Orcish Manuever section.

My code compiles cleanly, and it runs, but it's giving me incorrect results. The results I want are:

Unsorted List:beta, apple, gamma globulin, alpha, zeta, gesorenplatz!! +, gamma, delta, foolish mortal, Sorted List:, beta, zeta, alpha, apple, delta, gamma, foolish mortal, +gamma globulin, gesorenplatz!!
I don't want to spoil the exercise for those who haven't worked it yet, so I've spoilerized it...

I'm certain it's something simple, but I can't see it. Any help?

UPDATE: I forgot to wrap the code in code tags!
UPDATE: I linked "Orcish Manuever" since planetscape told me how.

TIA, roboticus

Replies are listed 'Best First'.
Re: Sorting problem with Orcish Manuever
by japhy (Canon) on May 22, 2006 at 12:26 UTC
    It's your return() statement. You've got return (X) or (Y) which Perl reads as (return(X)) or (Y), and since return() ALWAYS returns, you never get to the second clause. Rewrite it as return ((X) or (Y)) or, in your specific case, return X or Y.

    And if you find any typos or errors otherwise in the article, please let me know. A problem I noticed looking at it right now is that the orcish maneuver should really be:

    my $data_a = exists($cache{$a}) ? $cache{$a} : ($cache{$a} = mangle($a +)); my $data_b = exists($cache{$b}) ? $cache{$b} : ($cache{$b} = mangle($b +));
    to allow for false and undefined values to be returned by mangle().

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      That's no longer "Orcish", is it? "Orcish" (for "or-cache, implies use of "||=". The fact that some stuff may return false and thus be recalculated, is something you have to weigh against the ease of coding.

      Usually, it's rather rare. In this case, you will never get a false value back. So it's completely unnecessary.

      japhy:

      Thanks! I stared at that routine until I was blue in the face. I originally left the parentheses out, but had an entirely different bug in an earlier version. Then I left in the parentheses in 'cause I thought it looked good and wouldn't affect the value.
      <headsmack force="5">D'oh!</headsmack>

      --roboticus

Re: Sorting problem with Orcish Manuever
by bart (Canon) on May 22, 2006 at 12:35 UTC
    japhy got it nailed, just before I did. But my solution is different: replace
    return ($va->[0] <=> $vb->[0]) or ($va->[1] cmp $vb->[1]);
    with
    return ($va->[0] <=> $vb->[0]) || ($va->[1] cmp $vb->[1]);
    and it works.

    p.s. Please don't make us hunt around for the question so much. When I finally found exercise 2 in that section, it simply referred me to another exercise in a different section! How hard is quoting a question of 2 lines?

      bart:

      Thanks for the feedback. I'll give your version a try too.

      Regarding your P.S., sorry about that, my bad. I actually tried to link to it directly using syntax like [id://128722#the orcish manuever|Orcish Manuever], but it didn't work, and I couldn't figure out how to do it. I'll try to remember to add context in the future.

      Drat! And here I was patting myself on the back for actually reading the How (Not) To Ask A Question page (and others)... 8^P

      --roboticus

        Just an FYI: To link to an anchor like you intended, use this syntax:

        [link://?node=128722#the%20orcish%20maneuver|The Orcish Maneuver]

        The above produces: The Orcish Maneuver :-)

        HTH,

        planetscape
Re: Sorting problem with Orcish Manuever
by liverpole (Monsignor) on May 22, 2006 at 12:27 UTC
    You're getting exactly the results that one would expect, given that ...

    Update:  Never mind (must drink more coffee).  I actually tried the code, but "fixed" it inadverently (the way japhy describes above), then mistook the "expected results" for the actual results.  Forget I said anything.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
SUMMARY: Sorting problem with Orcish Manuever
by roboticus (Chancellor) on May 23, 2006 at 11:49 UTC
    Well, the problem turned out to be operator precedence in my return statement, as indicated by japhy and bart. Basically, using:

    return $va->[0] <=> $vb->[0] || $va->[1] cmp $vb->[1];
    or:

    return ($va->[0] <=> $vb->[0] or $va->[1] cmp $vb->[1]);
    does the trick.

    japhy also pointed out an improved version of the Orcish Maneuver, while bart suggests that the improved version needs a new name, and that I should link to the subtopic so helpful PerlMonks don't have to chase wild geese in order to help me. And another thank you to planetscape for showing me how to properly link to a subtopic here.

    Thanks all!

    --roboticus