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 - $tolerance; for (@nlist) { last if $_ > $centroid + $tolerance; push @{$hoa{$key}}, $_; } } print "$_ => [@{$hoa{$_}}]\n" for sort keys %hoa; #### 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]