in reply to "numeric" sort of keys?

You need to remove 'Drive' from the comparison so that perl can make a number from it. sub mysort { substr($a,5) <=> substr($b,5) } That could probably benefit fram a Schwartzian Transform.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: "numeric" sort of keys?
by Gorio3721 (Acolyte) on Sep 25, 2003 at 20:54 UTC

    This is exactly what I asked for BUT, I left out some detail on the structure of the keys. The text "field" of the key can vary in length and it is not always the word "Drive". My current list has Drive, IRMA, CMS, PRD and some others as the first "field" of the key and one or two digits as the second "field" of the key. The key is a combination of text and numbers and I want to sort first on the text "field" (which is not unique) and then sort on the numeric "field" of the key. Your help has pointed me in the right direction though. I see how I can iterate through each character until I find the first numeric character and calculate which column to split on. Or can I do this with some sort of regex and the split command? I'll go work it out and post my solution. Thanks to all for the help!!!!

    Gorio
      Gorio3721,
      My first attempt at the Schwartzian Transform.
      #!/usr/bin/perl -w use strict; my @keys = qw (CMS5 Drive1 Drive2 Drive10 IRMA1 IRMA13 IRMA2); my %hash; @hash{@keys} = (); for my $key ( custom_sort(\%hash) ) { print "Key: $key\n"; } sub custom_sort { my $hash = shift; return map { $_->[0] } sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]} map { [ $_, /(\w+?)(\d+)$/ ] } keys %$hash; } __END__ Key: CMS5 Key: Drive1 Key: Drive2 Key: Drive10 Key: IRMA1 Key: IRMA2 Key: IRMA13

      Cheers - L~R

Re: Re: "numeric" sort of keys?
by Gorio3721 (Acolyte) on Sep 25, 2003 at 20:58 UTC

    Oh, one more thing.

    What the heck is a Schwartzian Transform?

    Thx,

    Gorio