### place floats into array

by baxy77bax (Deacon)
 on Sep 19, 2013 at 18:01 UTC Need Help??
baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

Help. I am fealing extreamly stupid righ now and cannot solve this simple problem (no code necessary just a pointer)
What i have is a set of numbers
```1.3e-3
3.7e-5
102
46
1e-4
1e-12
all numbers are 10x larger or smaller then any other number in the group(i cannot even express myself correctly). What i need is to place them in the array depending on how meny decimal places they occupy given that if they are > 1 the an ofset by the largest number has to be takon in considereation:

table was updated  \$a[2] was missing , sorry, thnx choroba !!

```\$a[0] = 102;  #1e2
\$a[1] = 46;   #1e1
\$a[2] = 0;    #1e0
\$a[3] = 0;    #1e-1
\$a[4] = 0;    #1e-2
\$a[5] = 1.3e-3#1e-3
\$a[6] = 1e-4  #1e-4
\$a[7] = 1e-5  #1e-5
...
\$a[14] = 1e-12 #1e-12

how do i do this without using those chaines of if-else blocks

thnx

Replies are listed 'Best First'.
Re: place floats into array (Corrected.)
by BrowserUk (Pope) on Sep 19, 2013 at 18:25 UTC

Perhaps the word you are missing is 'magnitude'.

Try this:

```#! perl -slw
use strict;

sub log10{ log( \$_[0] ) / log( 10 ) }
sub magnitude{ int log10( \$_[0] ) }
sub sign { \$_[0] <=> 0 }

my @nums = sort{
\$b <=> \$a
} qw[ 1.3e-3 3.7e-5 102 46 1e-4 1e-12 ];

my @byMag;
my \$prev = \$nums[ 0 ];
for my \$i ( 1 .. \$#nums ) {
my \$this = \$nums[ \$i ];
my \$diff = abs( magnitude( \$prev ) - magnitude( \$this ) );
--\$diff if sign( magnitude( \$this ) ) == sign( magnitude( \$prev )
+);

push @byMag, \$prev;
push @byMag, ( 0 ) x \$diff if \$diff > 1;
\$prev = \$this;
}
push @byMag, \$prev;

print for @byMag;

__END__
C:\test>1054890.pl
102
46
0
0
0
1.3e-3
1e-4
3.7e-5
0
0
0
0
0
0
1e-12

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Is there an emoticon for "light bulb over head"? This makes a lot more sense than my interpretation of the question.
Re: place floats into array
by Perlbotics (Chancellor) on Sep 19, 2013 at 18:37 UTC

And another one...

```use strict;
use warnings;

my @nums = ( 1.3e-3, 3.7e-5, 102, 46, 1e-4, 1e-12);

@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   #1e-1
\$res[ 4] =        0   #1e-2
\$res[ 5] =   0.0013   #1e-3
\$res[ 6] =   0.0001   #1e-4
\$res[ 7] =  3.7e-05   #1e-5
\$res[ 8] =        0   #1e-6
\$res[ 9] =        0   #1e-7
\$res[10] =        0   #1e-8
\$res[11] =        0   #1e-9
\$res[12] =        0   #1e-10
\$res[13] =        0   #1e-11
\$res[14] =    1e-12   #1e-12

Re: place floats into array
by Anonymous Monk on Sep 19, 2013 at 23:10 UTC

Using sprintf:

```use strict;
use warnings;

my @num = qw[ 1.3e-3 3.7e-5 102 46 1e-4 1e-12 ];

my @a;
my \$base;

for (sort {\$b <=> \$a} @num) {
my (\$i) = sprintf('%e', \$_) =~ /([-+]\d+)/;
\$base //= \$i;
\$a[\$base - \$i] = \$_;
}

say \$_ // 0 for @a;

Results:

```102
46
0
0
0
1.3e-3
1e-4
3.7e-5
0
0
0
0
0
0
1e-12
Re: place floats into array
by keszler (Priest) on Sep 19, 2013 at 18:21 UTC
```use strict;
use warnings;
my @a = qw(
1.3e-3
3.7e-5
102
46
1e-4
1e-12
);
# force array element evaluation as numeric before sort
\$_ += 0 for @a;
print \$_,\$/ for sort { \$a <=> \$b } @a;
__END__
1e-12
3.7e-05
0.0001
0.0013
46
102
Re: place floats into array
by choroba (Bishop) on Sep 19, 2013 at 18:20 UTC
I do not get it. Where is \$a[2]? Do you know that 0.1 == 0.099999999999999999999999999999999?
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1054890]
Approved by BrowserUk
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2018-05-28 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (199 votes). Check out past polls.

Notices?