in reply to placing values into bins

The numbers you give in your example suggest that you may be able to make some simplifying assumptions. In particular, the (one!) example of a bin range has ranges for x and y which are exactly one whole number wide. If you can say that that is true for all bins, then it is possible to calculate directly which bin a datum should go in: simply apply int() to x and to y. This assumption also makes it reasonable to use arrays rather than hashes to store the bins. So:
my @data = ( [ 123.7, 456.7 ], [ 564.7, 234.9 ], ); for my $datum ( @data ) { my( $x, $y ) = @$_; $bins[ int $x ][ int $y ]++; } for my $x ( 0 .. $#bins ) { defined $bins[$x] or next; for my $y ( 0 .. $#{$bins[$x]} ) { defined $bins[$x][$y] or next; print "$x $y $bins[$x][$y]\n"; } }

Replies are listed 'Best First'.
Re^2: placing values into bins
by Anonymous Monk on Apr 28, 2005 at 12:59 UTC
    Hi there. Thanks for your replies so far. Much appreciated.
    But what if I wanted to choose 125.5-130.0 and 456.5-457.0 as a range for example? How simple would that be to do?

      If you follow the approach I sketched out in my other reply, all you need to do is pick the "left ends" of the ranges (in this case 125.5 and 456.5), and the desired bin widths; perl takes care of the right ends of the ranges depending on the actual data.

      the lowliest monk

      if you want to use the int method, but decide you want to use different sized bins, my simple solution would to be to create a new sub that returned the left end of the range, and just replace 'int' with that sub.