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!


In reply to Re: using perl to find words for scrabble by jcb
in thread using perl to find words for scrabble by Aldebaran

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.