in reply to Re: Clustering Numbers with Overlapping Members
in thread Clustering Numbers with Overlapping Members

Your method does not work for $tolerance > 1 - at least not how I understand the question :)

My solution is based on the OP's statement

So in principle every element in the array will be a centroid (except the first one, in this case it is 0).
use warnings; use strict; my @nlist = (0,1,2,3,4,5,6,8,10); my @key_list = ('A'..'Z'); my $tolerance = 2; my %hoa; for my $i (1..$#nlist) { my $key = shift @key_list; $hoa{$key} = [grep in_range($nlist[$i], $_), @nlist ]; } print "$_ => [@{$hoa{$_}}]\n" for sort keys %hoa; sub in_range { my ($centroid, $testnum) = @_; return abs($centroid - $testnum) <= $tolerance; }
which outputs (with tolerance 2):
A => [0 1 2 3] B => [0 1 2 3 4] C => [1 2 3 4 5] D => [2 3 4 5 6] E => [3 4 5 6] F => [4 5 6 8] G => [6 8 10] H => [8 10]

-- Hofmator