in reply to using perl to find words for scrabble

I suggest a linear scan over a wordlist to select candidate words that contain only letters in your set. For a first pass, you could build a regex as my $pat = qr/^[$letters]+$/. You will also probably find it easier to enter a string than to directly fill in an array, and you can produce your @letters array with @letters = split //, $letters. This also means that your letters can be passed as the first command-line argument and picked up with my $letters = shift @ARGV; instead of editing the script for every turn.

So we are at: (omitting boilerplate like use strict; and use warnings; because those should go without saying)

my $letters = shift @ARGV; my @letters = split //, $letters; my $pat = qr/^[$letters]+$/;

Now we have to open our wordlist file and filter out words that use letters we do not have:

my $wordlist = 'words.en.list'; # assumed English wordlist my @words = (); { open my $words, '<', $wordlist or die "$wordlist: $! $^E"; while (<$words>) { push @words, $_ if m/$pat/ } close $words; }

Now @words contains every legal word that we can form, and quite a few that we cannot. For example, if we have the letters "agrtuivbo", @words will contain "boot", which we cannot play, and "tuba", which we can. The other problem is the existing state of the board: what other letters can we play off of that we do not ourselves have? For simplification, I will assume that you are not actually playing Scrabble, and can only play words using letters in your current hand.

For the next step, we must sieve by letter counts and here is an untested sub that will count letters in a word:

sub count_letters ($) { my $word = shift; my %word = (); $word{$_}++ for split //, $word; return \%word; }

I will leave resolving whether the letters in a word are playable given the letters that we have to the reader. (Hint: $letters is also a "word"!) Once you have a playable_word_p sub, you can filter @words with: grep {playable_word_p $letters, $_} @words.

Enjoy!