At 4:12 PM +0100 8/5/03, 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

In the case you show (where the first three characters are all 'sar'), you could simply sort on the entire key. I'll assume, however, that the prefix varies. Assuming, as well, that the length of the prefix is always three, you should be able to use:

<science fiction>
  sub by_middle { substr($a, 3, 4) cmp substr($b, 3, 4) }
  my @keys = sort by_middle keys(%gene_pool);
</science fiction>

If the keys are less regular, you'll have to work harder to extract them.

-r
--
email: [EMAIL PROTECTED]; phone: +1 650-873-7841
http://www.cfcl.com/rdm    - my home page, resume, etc.
http://www.cfcl.com/Meta   - The FreeBSD Browser, Meta Project, etc.
http://www.ptf.com/dossier - Prime Time Freeware's DOSSIER series

Reply via email to