From what I gather you want everything numerical only up front, followed by anything with a letter in it sorted asciibetically. Here is some code using the ever popular
Schwartzian Transform:
use strict;
use warnings;
my %keys;
@keys{('a'..'n')}= ('2','1','12','a12','a9',
'a2b3','a2b4','b2b4','1a2','a2bb5',
'a2bb5a','a2bb5b',654,9);
my @foo = map { $_->[0] }
sort newsort
map { [$_, $keys{$_} =~ /^(\d+)$/ ? ($keys{$_},1)
: $keys{$_}
]
} keys %keys;
print "KEY:$_ VALUE:$keys{$_}\n" foreach (@foo);
sub newsort {
if ( defined $a->[2] and defined $b->[2] ){
if ($a->[1] > $b->[1]) {return 1}
return -1;
}
elsif ( defined $b->[2] or defined $a->[2]) {
if ( defined $a->[2] ) { return -1 }
return 1;
}
elsif ( $a->[1] gt $b->[1] ) { return 1; }
elsif ( $b->[1] gt $a->[1] ) { return -1; }
return 0;
} #newsort
__DATA__
KEY:b VALUE:1
KEY:a VALUE:2
KEY:n VALUE:9
KEY:c VALUE:12
KEY:m VALUE:654
KEY:i VALUE:1a2
KEY:d VALUE:a12
KEY:f VALUE:a2b3
KEY:g VALUE:a2b4
KEY:j VALUE:a2bb5
KEY:k VALUE:a2bb5a
KEY:l VALUE:a2bb5b
KEY:e VALUE:a9
KEY:h VALUE:b2b4
-enlil
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.