in reply to Why programming is so much more than writing code

Well, I can't comment on the meaningfullness of the name as you've removed it, but the intent of the code is clear. Even if the formatting is a bit awkward. (operators at the end of the lines? Come on! This ain't Ruby ... you don't have to hint the compiler that we are not done with the statement yet!)

This, in my humble opinion, rather readable snippet of code is supposed to be used by the sort() routine and specifies that the AoA is to be sorted by 3rd, 2nd, 1st, 0th and 4th columns in this order of importance. You might want to use constants instead of the numbers to specify the columns, but that's about the best you can do.

Unless of course you want to factor out the indices and use something like

sub SUB_NAME { for (3,2,1,0,4) { # sort by the specified columns my $res = ($a->[$_] <=> $b->[$_]) and return $res; } }
which would definitely need some explanation. Other than that whenever you see <=> or cmp, especially if comparing (some part of) $a and $b, think "sort".

Update: Oops. Good catch merlyn, thanks. A reminder to test the code I post here.

Replies are listed 'Best First'.
Re^2: Why programming is so much more than writing code
by merlyn (Sage) on May 07, 2007 at 21:44 UTC
    my $res = ($a->[$_] <=> $b->[$_]) and return $res;
    Which doesn't work because $res isn't declared until after the end of the statement! Doh!

    Under use strict, this would fail, unless you happened to have another $res in scope, and then it'd be returning that, some of the time. Spooky.

Re^2: Why programming is so much more than writing code
by GrandFather (Saint) on May 07, 2007 at 21:57 UTC

    Update: Argh! see blazar's reply below

    Better would be:

    sub SUB_NAME { # sort by the specified columns $_ = $a->[$_] <=> $b->[$_] and return $_ for 3, 2, 1, 0, 4; return 0; } }

    ;)


    DWIM is Perl's answer to Gödel
      Better would be:
      sub SUB_NAME { # sort by the specified columns $_ = $a->[$_] <=> $b->[$_] and return $_ for 3, 2, 1, 0, 4; return 0; }
      Modification of a read-only value attempted at 614055.pl line 3.