in reply to Grouping numbers

I've enjoyed pondering this question. There is a module on CPAN that seems appropriate. It is Set::Partition::SimilarValues. It was a little tricky to figure out how to get it to partition in a way that matched your needs, but this seems to do the trick. You may have to adjust the GroupSeparationFactor to suit your needs, but the rest is pretty much automatic. Here's a snippet:

use strict; use warnings; use Data::Dumper; use Set::Partition::SimilarValues; chomp( my( @numbers ) = <DATA> ); my $set_obj = Set::Partition::SimilarValues->new( GroupSeparationFactor => 1.15 ); my @sets = $set_obj->find_groups( @numbers ); print Dumper \@sets; __DATA__ 100.20 100.23 100.35 122.45 122.55 122.67 122.75 145.88 145.97 146.01 146.10

...and the output...

$VAR1 = [ [ '100.20', '100.23', '100.35' ], [ '122.45', '122.55', '122.67', '122.75' ], [ '145.88', '145.97', '146.01', '146.10' ] ];

Enjoy!


Dave

Replies are listed 'Best First'.
Re^2: Grouping numbers
by BrowserUk (Patriarch) on Jul 10, 2006 at 07:38 UTC

    This doesn't quite meet the OP's spec. which calls for boundaries limited by a absolute difference from the mean, whereas the group separation factor is a factor. Eg.

    122.45 => 0.155* greater than 0.15 122.55 => 0.055 122.67 => 0.065 122.75 + => 0.145 ====== 490.42 /4 = 122.605

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.