in reply to Clustering Numbers with Overlapping Members
Assuming no duplicates the following may do what you want:
use warnings; use strict; my @nlist = (0,1,2,3,4,5,6,8,10); my @key_list = ('A'..'Z'); my $tolerance = 1; my %hoa; while (@nlist) { my $key = shift @key_list; my $previous = shift @nlist; last if ! @nlist; my $centroid = $nlist[0]; @{$hoa{$key}} = ($previous) if $previous >= $centroid - $toleranc +e; for (@nlist) { last if $_ > $centroid + $tolerance; push @{$hoa{$key}}, $_; } } print "$_ => [@{$hoa{$_}}]\n" for sort keys %hoa;
Prints:
A => [0 1 2] B => [1 2 3] C => [2 3 4] D => [3 4 5] E => [4 5 6] F => [5 6] G => [8] H => [10]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Clustering Numbers with Overlapping Members
by Hofmator (Curate) on Aug 07, 2006 at 12:25 UTC |