And I would like to generate a list ordered by the \d\d\d\d bit in the middle. I have this from the perl cookbook
my @keys = sort {criterion()} keys(%gene_pool);
but I don't really know how to approach the criterion() function
The criterion function takes two elements from the list (referred to inside the criterion function by the special package global variables $a and $b) and says whether $a comes before or after $b, or whether it doesn't matter which comes first.
You don't feed the criterion function the elements--perl's sort function takes the criterion function and applies it to pairs of list-to-be-sorted elements, as necessary.
Your sort function says which element comes first by returning (I think I have the direction right...)
-1 if $a comes before $b, 1 if $b comes before $a, or 0 if they are equivalent by the sort criteria.
The cmp and <=> operators usually do the last step of deciding which result to return, since they do that kind of thing well. But you can calculate the result otherwise.
There are a few things to watch out for:
Don't try to make $a and $b lexical variables in the criterion function. You want the globals.
If you're doing this sorting in a package, you have to qualify which $a and $b you're talking about with the package name.
These $a and $b variables are passed by reference, so you don't want to change them in your criterion function. If you do, you'll change the elements in the original list, and they won't look like the hash keys anymore. For the kind of sort you describe, you want to create local variables in the criterion function from the relevant portions of the original $a and $b. Then you crunch the local variables to decide whether $a or $b should come first and return -1, 1, or 0 as appropriate.
Be sure your criterion functions can't produce inconsistent results (x before y, y before z, z before x...) Bad things will happen.
Efficiency can be a problem, especially if you're sorting lots of genes.
Finally, you might want to include a fallback sort if the main one fails for some reason.
--Shawn