use Math::Combinatorics; my $nwordsatonce = 4; my ($k,$v); my %totals = (); local $" = ' '; # just in case while (($k,$v) = each %items) { next unless $nwordsatonce <= @$v; my @words = sort @$v; do {$totals{"@$_"} += 1;} for combine($nwordsatonce,@words); } my @comb = sort {$totals{$b} <=> $totals{$a}} keys %totals; print "Top $nwordsatonce - word combinations:\n"; do {print "$_\n";} for @comb[0..4];