in reply to sort question

It's a work for Schwartzian Transform...
$entry{$table}{A}{RANK} = R#3; $entry{$table}{C}{RANK} = R#2; $entry{$table}{B}{RANK} = R#1; @array = map { $_->[0] } sort { $b->[1] cmp $a->[1] } map { [ $_, $entry{$table}{$_}{RANK} ] } keys %{$entry{$table}}; print ">> @array <<\n"; __END__ >> A C B <<
Update:
  • Change sort from <=> to cmp.
  • Dragonchild did one more single and efficient code!

    --
    Marco Antonio
    Rio-PM

  • Replies are listed 'Best First'.
    Re^2: sort question
    by rvosa (Curate) on May 17, 2005 at 18:51 UTC
      Hey mda2, I tweaked it a bit and it works, but I have NO idea why or how:
      sub sort_ranks { my @array = map {$_->[0]} sort {($a->[1]=~/^R#(\d+)$/)[0] <=> ($b->[1] +=~/^R#(\d+)$/)[0]} map {[$_,$entry{$table}{$_}{RANK}]} @_; }
      In fact, I think I'll use this as my JAPH ;-)
        if you don't know how it works, i really hope you're not using it for anything important! otherwise you'll be in trouble when you try to maintain it in a couple months! "now why did i do it like this... well, turns out i didn't even know then, and i'm more clueless now!"

        this is what is called a Schwartzian Transform, so check out 441762 and http://www.sysarch.com/perl/sort_paper.html
          Hey...okay... interesting. Now I know what a Schwartzian Transform is. I've seen it mentioned before, but now I know. Thanks, boris!
          Actually, the main issue is that I rarely use 'map', and I should, so this is a good learning experience.