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:
>
> 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
> );
>
> foreach my $key (sort(keys(%mdata)))
> {
> print "$key\n";
> }
>
> The output of this code is as follows, and you can see that the sort
> order is not what I wanted:
>
> CALIBRATION02
> CALIBRATION1
> LABVIEW-1
> LABVIEW-11
> LABVIEW-114
> LABVIEW-12
> LABVIEW-2
> LABVIEW-4
> YESTECH-L03-RW2
> YESTECH-L3-RW1
> YESTECH-L4-RW125
>
> Any ideas on how to get this to come out in the "right" order?
When you say "It is my desire that the machine names be processed in the
order that they have been loaded into the hash", it suggests that your
question is not about sorting at all. Indeed, it is a FAQ, see 'perldoc
-q "hash.*order"'.
If your question really is about sorting, then a more accurate
description of the keys and how to comare them is needed. However, the
following sort function, which splits the keys into strings of digits
and non-digits for comparison seems to work for the data provided.
use strict;
use warnings;
use List::Util qw{shuffle};
sub sortfun ($$) {
my ($v1, $v2) = @_;
my @v1 = split /(\d+)/, $v1;
my @v2 = split /(\d+)/, $v2;
while (@v1 and @v2) {
my $val1 = shift @v1;
my $val2 = shift @v2;
if ($val1 =~ /^\d+$/) {
if ($val1 != $val2) {
return $val1 <=> $val2;
}
}
elsif ($val1 ne $val2) {
return $val1 cmp $val2;
}
}
return @v1 <=> @v2;
}
my @data = <DATA>;
chomp @data;
my @result = sort sortfun @data;
print join("\n", @result), "\n\n";
print "Now after a shuffle\n";
@data = shuffle(@data);
print join("\n", @data), "\n\n";
print "Results in:\n";
@result = sort sortfun @data;
print join("\n", @result), "\n\n";
__DATA__
CALIBRATION1
CALIBRATION02
LABVIEW-1
LABVIEW-2
LABVIEW-4
LABVIEW-11
LABVIEW-12
LABVIEW-114
YESTECH-L3-RW1
YESTECH-L03-RW2
YESTECH-L4-RW125
HTH
--
Brian Raven
Please consider the environment before printing this email.
This e-mail may contain confidential and/or privileged information. If you are
not the intended recipient or have received this e-mail in error, please advise
the sender immediately by reply e-mail and delete this message and any
attachments without retaining a copy.
Any unauthorised copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs