the_slycer has asked for the wisdom of the Perl Monks concerning the following question:
Fellow monks,
I'm in the midst of a rewrite of a piece of code that is completely unflexible. One of the things that I'm attempting to do in adding flexibility to the code, is allow sorting based on a coderef.
So, I have a many dimensional hash that I'm using to setup parameters for how something should work. I would like to have one of the (optional) keys along the way be a 'sort' key with a coderef as the value.
This is relatively easy to do, the problem comes in when I am attempting to call a sort routine that accepts parameters..
I guess, because this is a new script, I could rethink how I'm building the list(s) that will need sorting, but my preference would be to somehow get the above going.
Thanks in advance for any advice.
I'm in the midst of a rewrite of a piece of code that is completely unflexible. One of the things that I'm attempting to do in adding flexibility to the code, is allow sorting based on a coderef.
So, I have a many dimensional hash that I'm using to setup parameters for how something should work. I would like to have one of the (optional) keys along the way be a 'sort' key with a coderef as the value.
This is relatively easy to do, the problem comes in when I am attempting to call a sort routine that accepts parameters..
The above works fine, sorts based on $a/$b comparison/whatever. The problem is, when I want to sort (for example) a hash based on value. The sort routine needs the hash..$parameter_hash->{$this}{$value}{sort_by} = \&custom_sort; ... later, in some other subroutine ... my $sort_routine = $parameter_hash->{$this}{$value}{sort_by} || #other + standard sorts here; foreach (sort $sort_routine keys %{ $parameter_hash->{$this}{$value}{v +alue_list} }) { #do stuff }
This fails with a syntax error, as does:foreach (sort $sort_routine->(\%href) keys %{ $somehashref }) { #do stff }
and other variations of the sort.sort &{ $sort_routine }(\%href); sort &{ $sort_routine(\%href) };
I guess, because this is a new script, I could rethink how I'm building the list(s) that will need sorting, but my preference would be to somehow get the above going.
Thanks in advance for any advice.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Sorting using coderef's, and passing parameters
by tilly (Archbishop) on Aug 17, 2004 at 22:48 UTC | |
Re: Sorting using coderef's, and passing parameters
by Fletch (Bishop) on Aug 17, 2004 at 20:32 UTC |
Back to
Seekers of Perl Wisdom