in reply to decimal numbers as hash keys
If you need a particular format of your number in order to get a desired behavior, you need to control how your number gets converted into a string. This is usually done using sprintf. For example
will cause the key accessed to be a floating point number with 3 decimal places, thus forcing the numbers to yield appropriate strings. It also avoids the potential 6.269999999999999999 problem.$ifac{sprintf "%.3f" $factor}
In your case, your issue begins with your hash creation:
printsuse Data::Dumper; my %ifac= ( 14.152 => "JCI", 9.334 => "JHEP", 5.745 => "JIMM", 6.270 => "JID", 1.480 => "JRN", 6.105 => "KI" ); print Dumper \%ifac;
You need to control string format on input, which means using strings not numbers for initialization:$VAR1 = { '6.105' => 'KI', '1.48' => 'JRN', '9.334' => 'JHEP', '5.745' => 'JIMM', '6.27' => 'JID', '14.152' => 'JCI' };
my %ifac= ( '14.152' => "JCI", '9.334' => "JHEP", '5.745' => "JIMM", '6.270' => "JID", '1.480' => "JRN", '6.105' => "KI" );
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: decimal numbers as hash keys
by Alex31 (Initiate) on Jun 26, 2013 at 15:53 UTC | |
by kennethk (Abbot) on Jun 26, 2013 at 18:11 UTC |
In Section
Seekers of Perl Wisdom