Okay.. I was bored and wanted to write some stupid script that made use of the dictionary file. I also really suck at crossword puzzles. So I came up with this little script to help me out.
#!/usr/local/bin/perl -w
use strict;
my $word_file = '/usr/share/dict/words';
my @words = ();
my $word_length;
my @tests;
if (@ARGV == 2) {
$word_length = shift;
my $tests = shift;
@tests = split /,/,$tests;
print "\$word_length = $word_length\n";
print "\@tests = @tests\n\n";
}elsif (@ARGV ==1) {
$word_length = shift;
print "\$word_length = $word_length\n";
}else {
die "usage: $0 word_len [test1],[test2] etc..\n",
" $0 word_len\n\n",
"example: $0 3 [1=a],[3=s]\n",
" find 3 letter word starts with 'a' ends with 's'\n\n",
" $0 14\n",
" find 14 letter word.\n\n";
}
open (INP, $word_file) or die "Error opening word database: [$word_fil
+e] $!\n";
while (<INP>){
chomp;
push @{$words[length]}, lc;
}
close (INP);
my @chk = get_words($word_length);
die "No Words that long.\n" unless @chk;
foreach my $word (@chk) {
if (@tests) {
print "$word\n" if check_word($word,@tests);
}else{
print "$word\n"
}
}
sub check_word {
my $word = shift;
my @tests = @_;
my @letters = split //, $word;
foreach my $test (@tests){
my ($num,$let) = $test =~ /^\[(\d+)=(\w)\]$/;
--$num;
return unless $letters[$num] eq $let;
}
return 1;
}
sub get_words {
my $len = shift;
return @{$words[$len]} if exists $words[$len];
return;
}