Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Sorting using coderef's, and passing parameters

by the_slycer (Chaplain)
on Aug 17, 2004 at 20:28 UTC ( #383789=perlquestion: print w/replies, xml ) Need Help??

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..
$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 }
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..
foreach (sort $sort_routine->(\%href) keys %{ $somehashref }) { #do stff }
This fails with a syntax error, as does:
sort &{ $sort_routine }(\%href); sort &{ $sort_routine(\%href) };
and other variations of the sort.

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
    One approach is to say that any sort routine should never need an additional value to be passed in. Any case which looks like it needs that can have the assumption solved by prebinding an argument with a closure.
    my $real_sort_routine = sub {$sort_routing->(\%href)}; foreach (sort $real_sort_routine keys %{ $somehashref }) { #do stff }
    Now that you've moved the binding elsewhere, you can choose to put the binding wherever it makes sense - possibly during building up the definitions of sorts that you need.

    You can find an example of setting up a complex sort using closures at Re: Fun with complex sorting on arbitrary criteria list..

    A useful trick to know: if you'll be building a sort function in a different package than you're calling sort in, you'll want your functions to all have a prototype of ($$). With 5.6 or better, that will cause Perl to pass $a and $b in rather than setting appropriate globals in the package that sort was called from. (Which might not be the package that the sort function looks in - very confusing if it happens to you.)

Re: Sorting using coderef's, and passing parameters
by Fletch (Chancellor) on Aug 17, 2004 at 20:32 UTC

    foreach( sort { $sort_routine->( \%href ) } keys %{$someref} ) { ... }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://383789]
Approved by Crackers2
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2020-06-01 23:34 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (12 votes). Check out past polls.