use Algorithm::Bucketizer; while (my $in = <>) { chomp($in); my %counts; $counts{$_}++ for split //, $in; my $b = Algorithm::Bucketizer->new(bucketsize=>length($in)/4); $b->add_item($_, $counts{$_}) for keys %counts; $b->optimize(maxrounds=>100); print $_->serial, " => ", join(", ", map {"$_ ($counts{$_})"} $_->items ), "\n" for $b->buckets; }