You could use a hash of hashes and (if your range is fixed) - use keys of either the high or low values.

For instance, if you were going to go low values, your structure would look like $hash->{123}->{456} for the example.

my $range = 1; my $hash = {}; while (<>) { chomp; my ($num1, $num2) = split ' ',$_; # key the numbers my $int_num1 = int( $num1 ); # drop the decimal # this will create the correct key for an integer range # e.g. if you had a range of 5, this would result in # 120 being the first bucket my $key1 = $int_num1 - ($int_num1 % $range); # combined steps for key2 my $key2 = int($num2) - (int($num2) % $range); $hash->{$key1}->{$key2}++; } # now you can get your indices foreach my $range1 ( keys %$hash ) { foreach my $range2 ( keys %{$hash->{$range1}} ) { print $range1."-".($range1+$range); print " "; print $range2."-".($range2+$range); print " "; print $hash->{$range1}->{$range2}; print "\n"; } }
This is untested, but I think (hope) it gives you at least an idea of one way to do it. Of course you can make it more efficient, shorter, etc.

In reply to Re: placing values into bins by Transient
in thread placing values into bins by Anonymous Monk

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.