shemp has asked for the wisdom of the Perl Monks concerning the following question:
sort of thing.foreach my $key ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) { # do something useful }
I will need to use a named sort function, specified at runtime, but something like this will not work:
Because %hash is not necessarily scoped in my_sorter()foreach my $key ( sort my_sorter keys %hash ) { # do something useful } ... ... sub my_sorter { return $hash{$a} cmp $hash{$b}; }
In my situation, it will be nearly impossible to have the hash being sorted in the proper scope directly.
What i realy want to do is pass anonymous sort subroutines into the function that needs to sort the hash, but i just dont see how to make this work properly. The hash itself will always be out of scope.
Note that this is a simplification of the real problem, the sorting by values problem is only part of the situation, but its the only part thats giving me trouble.
So i guess the real issue is: How do i sort by values without an inlined sort?
Thanks much - beating my head
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sort by value - new twist
by ikegami (Patriarch) on Sep 01, 2004 at 18:59 UTC | |
by shemp (Deacon) on Sep 01, 2004 at 19:29 UTC | |
by Limbic~Region (Chancellor) on Sep 01, 2004 at 22:07 UTC | |
by ihb (Deacon) on Sep 02, 2004 at 01:44 UTC | |
|
Re: Sort by value - new twist
by Aristotle (Chancellor) on Sep 01, 2004 at 19:16 UTC | |
|
Re: Sort by value - new twist
by demerphq (Chancellor) on Sep 01, 2004 at 19:30 UTC | |
by Aristotle (Chancellor) on Sep 01, 2004 at 21:38 UTC | |
by demerphq (Chancellor) on Sep 02, 2004 at 05:44 UTC | |
|
Re: Sort by value - new twist
by yosefm (Friar) on Sep 01, 2004 at 19:34 UTC | |
|
Re: Sort by value - new twist
by VSarkiss (Monsignor) on Sep 01, 2004 at 19:11 UTC | |
by shemp (Deacon) on Sep 01, 2004 at 19:27 UTC |