in reply to Sorting question
What you may need here is a "Schwartzian Transform", so that you can sort based on a transformed key, where the transform is to expand any group of digits into a wider, fixedsized group. In this way, "K2D1A" gets a sort key of "K002D001A", and "K2D10A" gets a sort key of "K002D010A". Since all sequences of numbers are now equally wide, you'll get the sort order you desire. But since the Schwartzian Transform holds on to unmodified data, you'll have the untransformed keys to display.
Here's an (untested) starting point:
my @data = qw(K2D1A K2D2A K2D10A);
my @sorted =
map { $_>[0] }
sort { $a>[1] cmp $b>[1] }
map { [$_, normalize_digits($_) ] }
@data;
sub normalize_digits {
my ($key) = @_;
$key =~ s/(\d+)/sprintf("%03d", $1)/eg;
return $key; # thanks, ikegami
}
This builds a composite data structure with the original key and a "normalized" key, sorts the structure based on the normalized key, and then discards the normalized key.
Update: Or, better yet, don't reinvent the wheel, and use Sort::Naturally (which I'd somehow overlooked).
Re^2: Sorting question by ikegami (Pope) on May 14, 2006 at 17:09 UTC 
Your code doesn't work because you forgot return $key; in normalize_digits.
Also, I think you can speed things up by using the following:
my @sorted = map { local $_ = $_; s/^.*\0//; $_ }
sort
map { normalize_digits($_) . "\0$_" }
@data;
 [reply] [d/l] [select] 
Re^2: Sorting question (slow) by tye (Sage) on May 14, 2006 at 16:58 UTC 
Note that your quite simple, dozenline reinvention is going to be a ton faster than the method used by Sort::Naturally (you can probably make it 2 to 4times faster still and more memory efficient by using a smarter sorting technique as well, such as my favorite or some found by natural sort). The file size might not make even these quite small increases in complexity in the code worthwhile, of course.
 [reply] 
Re^2: Sorting question by Scott7477 (Chaplain) on May 15, 2006 at 22:38 UTC 
Your explanation of the "Schwartzian Transform" is the easiest one to understand that I've seen so far. Nice++  [reply] 
