Monks,
I have inherited some code that does what would appear to be a simple binning operation. However, I have discovered an error in the binning but for the life of me don't see what's wrong with the code.
Given a set of data between 0.9 and 1, the code should place it in 12 different bins.
Bin 0 is < 0.9,
Bin 1 >= 0.9 && < 0.91,
Bin 2 >= 0.91 && < 0.92,
etc..
Bin 11 >= 1.0
When run, some values get shifted down, others are placed in appropriate bins. So far, I am concerned with the < operator because it shows for example, that 0.99 is < 0.99. I hope there is a stupid mistake that I am just missing; I'd appreciate any assistance. Whether the binning algorithm is efficient or not, I'm not currently concerned with, first and foremost is that it work.
Here's the code...
#!/usr/bin/perl
use strict;
my %final_data = (
'1' => 1.000,
'2' => 0.990,
'3' => 0.980,
'4' => 0.970,
'5' => 0.960,
'6' => 0.950,
'7' => 0.940,
'8' => 0.930,
'9' => 0.920,
'10' => 0.910,
'11' => 0.900,
'12' => 0.890,
);
my $min = 0.90;
my $max = 1.00;
my $low;
my $high;
my $incr = 0.01;
DATA_ITEM:
for my $key ( sort { $a <=> $b } keys %final_data )
{
$low = $min;
$high = $min + $incr;
for my $bin ( 1 .. 10 )
{
if( $final_data{$key} < $min )
{
warn "$final_data{$key} fell in bin 0 ( $final_data{$key} < $min
+)\n";
$low = $high;
$high += $incr;
next DATA_ITEM;
}
elsif( $final_data{$key} >= $max )
{
warn "$final_data{$key} fell in bin 11 ( $final_data{$key} >= $ma
+x )\n";
$low = $high;
$high += $incr;
next DATA_ITEM;
}
elsif( ($final_data{$key} >= $low) && ($final_data{$key} < $high
+) )
{
warn "$final_data{$key} fell in bin $bin ( $final_data{$key} >= $
+low && $final_data{$key} < $high )\n";
$low = $high;
$high += $incr;
next DATA_ITEM;
}
$low = $high;
$high += $incr;
}
}
Inelukii
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
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.