use strict;
use warnings;
my @nums = ( 1.3e3, 3.7e5, 102, 46, 1e4, 1e12);
@nums = sort { $b <=> $a } @nums; # biggest number first
my $exp_max = int log($nums[0]) / log(10); # first=biggest exponent
my $exp = $exp_max; # start exponent
my @res;
while ( @nums ) {
push @res, $nums[0] >= 10**$exp ? shift @nums : 0;
$exp;
}
printf( "\$res[%2d] = %8g #1e%d\n", $_, $res[$_], $exp_max$_) for (
+0..$#res);
Result:
$res[ 0] = 102 #1e2
$res[ 1] = 46 #1e1
$res[ 2] = 0 #1e0
$res[ 3] = 0 #1e1
$res[ 4] = 0 #1e2
$res[ 5] = 0.0013 #1e3
$res[ 6] = 0.0001 #1e4
$res[ 7] = 3.7e05 #1e5
$res[ 8] = 0 #1e6
$res[ 9] = 0 #1e7
$res[10] = 0 #1e8
$res[11] = 0 #1e9
$res[12] = 0 #1e10
$res[13] = 0 #1e11
$res[14] = 1e12 #1e12
