Barry Brevik wrote:
> I'm having a problem sorting data items that are alpha-numeric strings.
> I know how to do it if the string is all alpha or all numeric, but the
> combo eludes me.
>  
> Take as example the following code. It is my desire that the machine
> names be processed in the order that they have been loaded into the
> hash. This is an example only- the machine names will not actually be
> loaded in order. Also, there will not always be "dashes" separating the
> alpha from the numbers:

This is a little brute force and specific to your data, but may do the
trick:

use strict;
use warnings;

my %mdata = (
   'CALIBRATION1', 1,
   'CALIBRATION02', 1,
   'LABVIEW-1', 1,
   'LABVIEW-2', 1,
   'LABVIEW-4', 1,
   'LABVIEW-11', 1,
   'LABVIEW-12', 1,
   'LABVIEW-114', 1,
   'YESTECH-L3-RW1', 1,
   'YESTECH-L03-RW2', 1,
   'YESTECH-L4-RW125', 1,
);

sub mysort {

if ($a =~ /^([a-z-]+)(\d+)(.*)$/i) {

        my $a1 = $1;
        my $a2 = $2;
        my $a3 = $3;

        if ($b =~ /^([a-z-]+)(\d+)(.*)$/i) {

                my $b1 = $1;
                my $b2 = $2;
                my $b3 = $3;

                if ($a1 eq $b1) {
                        if ($a2 == $b2) {
                                return $a3 cmp $b3;
                        }
                        return $a2 <=> $b2;
                }
        }
}
$a cmp $b;

}

foreach my $key (sort mysort keys %mdata) { print "$key\n"; }

__END__
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to