On Tuesday, August 5, 2003, at 10:12 AM, Adam Witney wrote:
Hi,
I have a hash with keys of the format
sar0011_4 sar0203_3 sar0050_5 sar2001_1 sar0002_9
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
If all of them will start with the same prefix and contain 4 digits there, then you can just do
my @keys = sort keys %gene_pool;
If they don't, and you really need to sort by the \d\d\d\d, then the following would work:
sub by_id { my ($x, $y) = ($a =~ /(\d\d\d\d)/, $b =~ /(\d\d\d\d)/); $x <=> $y or $a cmp $b; # Fall back to comparison of entire key } my @keys = sort by_id keys %gene_pool;