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.

In reply to Re^3: 2D binning by austinby
in thread 2D binning by austinby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.