On Wednesday, August 6, 2003, at 12:31 am, Jeff Lowrey wrote:
At 04:12 PM 8/5/03 +0100, 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
Use substring to access the parts of the key you want to sort on.
in the spirit of TMTOWTDI
assuming the letters won't always be the same :
$test="sar0011_4"; (undef,undef,undef,$key)=split(/[A-Za-z]/,$test); print $key,"\n";
assuming the letters will always be the same:
$test="sar0011_4"; (undef,$key)=split(/sar/,$test); print $key,"\n";
or (still assuming the letters don't change) as has been already pointed out, just use sort on the numbers with the letters.
Robin