use Algorithm::Combinatorics qw(combinations); my %hash; my $iterator = combinations(\@tiles, $rack_size); while (my $rack = $iterator->next) { $hash{ join q(), @$rack }++; }