Code craft: June 2008 Archives
I'm in the middle of a game of Scrabulous with Christoper Humphries on Facebook, and I get "tolkien" handed to me in my tray. Good letters, and I ought to be able to make a bingo out of them. Alas, the best I could get to play on the board was "knot", but what else could I have made? Perl to the rescue!
All I need to do is match across the contents of /usr/share/dict/words in a Perl one-liner. The -n flag means "loop over the input file, but don't print $_". My little program goes in -e, and it looks like this:
$ perl -lne'print if /t/ && /o/ && /l/ && /k/ && /i/ && /e/ && /n/' /usr/share/dict/words allokinetic ankylopoietic anticlockwise automatonlike bibliokleptomania ....
Lots of good words, but they're awfully long. Let's limit it to seven-letter bingos. We have to use the -l flag to drop the linefeed from the input lines, so the length call is accurate.
$ perl -lne'print if /t/ && /o/ && /l/ && /k/ && /i/ && /e/ && /n/ && length($_)==7' /usr/share/dict/words $
Shoot, nothing there. Let's try eight.
perl -lne'print if /t/ && /o/ && /l/ && /k/ && /i/ && /e/ && /n/ && length($_)==8' /usr/share/dict/words knotlike townlike
"knotlike"! That would have been beautiful. Oh well. :-(