Doing the naïve thing does the right thing, albeit with warnings:
use warnings;
use diagnostics;
use strict;
my @array = ("8.foo", "6.bar", "7.baz",
"5.biz", "3.fizzle", "0.fro", "9.boz");
print join "\n", sort {$a <=> $b} @array;
__END__
Argument "8.foo" isn't numeric in sort at 376443.pl line 6 (#1)
(W numeric) The indicated string was fed as an argument to an oper
+ator
that expected a numeric value instead. If you're fortunate the me
+ssage
will identify which operator was so unfortunate.
Argument "6.bar" isn't numeric in sort at 376443.pl line 6 (#1)
Argument "7.baz" isn't numeric in sort at 376443.pl line 6 (#1)
Argument "5.biz" isn't numeric in sort at 376443.pl line 6 (#1)
Argument "3.fizzle" isn't numeric in sort at 376443.pl line 6 (#1)
Argument "0.fro" isn't numeric in sort at 376443.pl line 6 (#1)
Argument "9.boz" isn't numeric in sort at 376443.pl line 6 (#1)
0.fro
3.fizzle
5.biz
6.baz
7.bar
8.foo
9.boz
However, if you want to dispense with the warnings, you could use the "Schwarzian Transform" like this:
use warnings;
use diagnostics;
use strict;
my @array = ("8.foo", "6.bar", "7.baz",
"5.biz", "3.fizzle", "0.fro", "9.boz");
print join "\n",
map {$_->[0]}
sort {$a->[1] <=> $b->[1]}
map { [$_, by_number($_)] } @array;
sub by_number {
my $value = shift;
if ($value =~ m/^(\d+)/) {
return $1
}
}