I'm not exactly sure what you want (it would have helped if you had provided an
example) but here's what I use:
sub byfile {
my @a = split /(\d+)/, $a;
my @b = split /(\d+)/, $b;
my $M = @a > @b ? @a : @b;
my $res = 0;
for (my $i = 0; $i < $M; $i++) {
return -1 if ! defined $a[$i];
return 1 if ! defined $b[$i];
if ($a[$i] =~ /\d/) {
$res = $a[$i] <=> $b[$i];
} else {
$res = $a[$i] cmp $b[$i];
}
last if $res;
}
$res;
}
This may be more complicated than you need. Given the following:
53 7 119 53red
red5 red6 red7 red67 red6.jpg red12.jpg
green4.jpg
blue2.jpg blue1000.jpg blue2.jpg58
it produces the following output:
7
53
53red
119
blue2.jpg
blue2.jpg58
blue1000.jpg
green4.jpg
red5
red6
red6.jpg
red7
red12.jpg
red67
Lucs St. Louis also suggests:
while (defined (my $A = shift @a) and defined (my $B = shift @b)) {
$res = ($A =~ /\d/) ? $A <=> $B : $A cmp $B;
return $res if $res;
}
return defined $A ? -1 : 1;
Hope this helps.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|