Hi perlmonks,
I tested the code sent by spx2 and BrowserUk after installing the List::AllUtils and Data::Dump modules.
I get "4" as output from spx2's code.
#!/usr/bin/perl -slw
use strict;
use warnings;
use List::AllUtils qw/minmax/;
my @points = (
[3.70722,0.54617,],
[3.67342,0.64892,],
[3.75017,0.85075,],
[3.82831,0.83506,],
[3.71852,0.79251,],
[3.80706,0.70426,],
[3.91839,0.94094,],
[3.90726,0.81722,],
[3.89007,0.76107,],
[3.98006,0.62477,],
);
my $bins;
my $binside = 0.1;
my ($xmin,$xmax) = minmax( map { $_->[0] } @points );
my ($ymin,$ymax) = minmax( map { $_->[1] } @points );
for( my $xbin=$xmin ; $xbin<=$xmax ; $xbin+= $binside) {
for(my $ybin=$ymin ; $ybin<=$ymax ; $ybin+= $binside) {
my $p = $points[0];
if( $p->[0] >= $xbin && $p->[0] < $xbin + $binside &&
$p->[1] >= $ybin && $p->[1] < $ybin + $binside ) {
$bins->{$xbin.$ybin} = []
unless($bins->{$xbin.$ybin});
push @{ $bins->{$xbin.$ybin} } , (shift @points);
};
}
}
print scalar(keys(%$bins));
Not sure what the "4" means.
I get a "Global symbol "$Y" requires explicit package name at line " error with BrowserUk's code. When I change the positions of $Y and $X, I get a "Global symbol "$X" requires explicit package name at line "
#!/usr/bin/perl -slw
use strict;
use List::Util qw[ sum ];
use Data::Dump qw[ pp ];
our $Y //= 10;
our $X //= 10;
my @xs = map -3+rand 6, 1 .. $X;
my @ys = map -3+rand 6, 1 .. $Y;
my %bins;
for my $x ( @xs ) {
my $xc = ( int( $x * 10 ) + 0.5 ) / 10;
for my $y ( @ys ) {
my $yc = ( int( $y * 10 ) + 0.5 ) / 10;
++$bins{ "$xc:$yc" };
}
}
pp \%bins;
printf "Buckets used: %d, total values %d \n",
scalar keys %bins, sum values %bins;
There's a script online that does exactly what I'm trying to do (http://homepages.ulb.ac.be/~dgonze/SCRIPTS/PERL/histogram-2d.pl), but requires user-defined inputs. I'd like to do this such that all the user needs is the input file. |