[plug] scrabble regex

Arie Hol arie99 at ozemail.com.au
Fri Jun 20 22:20:35 WST 2003


What about something like this :

Pass all the letters you want checked as an array, then run that array
against the dictionary - check the first element of the array against the
first word - if matched try next element in array - (and so on) when a given
element is not matched in the word - discard the word and move to next word
- and repeat order of elements in array of letters. If all letters in the
array are matched in the word - output the word.


Might look something like this (I hope) :


foreach $word (@dictionary)
        {
        foreach $letter (@argv)
                {
                if !($word =~ m/@argv[$letter]/i)
                        {
                        last; # or exit or whatever.
                        }
                }
        print $word;
        }

I know the syntax and the logic both leave a lot to be desired - but I hope
you get my drift ??

You only look at each word once, as soon as one letter 'does not match' -
you look at the next word.

At end of the run you should (hopefully) have a list of words that contain
all the required letters.

I have not considered whether or not there may be more than one instance of
a letter in a word - as in 'ee', 'oo', 'mm', nn' or 'tt' or as in 'totter' -
'teeter' - 'forever' etc..

HTH














>but I didn't come up with anything better:
>
>foreach $letter (split /''/, $letters) {
>  $myletters{$letter}++;
>}
>
>and this is the inefficient part:
>
>foreach $word (@dict) {
>  foreach $letter (split /''/, $word) {
>    $dictletters{$letter}++;
>  }
>}
>
>and then you compare the two hashes. Wow, it's been ages since I've
>written any Perl.
>
>-- 
>Ville Johansson of  | ()  ascii ribbon campaign - against html mail   
>Tammisaari, Finland | /\                        - against microsoft
attachments 
>
>
Regards Arie

>--------------------------------------------<

For the concert of life, nobody has a program.

>--------------------------------------------<



More information about the plug mailing list