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
In reply to Re: Grouping numbers
by davido
in thread Grouping numbers
by tamaguchi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |