Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Crossword helper

by zzspectrez (Hermit)
on Jan 27, 2001 at 12:50 UTC ( [id://54732]=CUFP: print w/replies, xml ) Need Help??

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; }

Replies are listed 'Best First'.
Re: Crossword helper
by epoptai (Curate) on Jan 27, 2001 at 15:30 UTC
Alternative invocation
by orkysoft (Friar) on Jan 28, 2001 at 07:09 UTC
    Wouldn't it be easier on the user to specify the word to be found like this:

    cr...w..d

    The program can find the length of the word, and the known letters really easily. Actually, it's a regexp already! (But check it for naughty stuff first, like, delete all /[^A-Za-z.]/ chars since they're not supposed to appear in crosswords or the dictionary file.)

      FWIW, I just wrote a BASH script that takes input like:

      cr...w..d

      Very simple and works well. Somehow I don't think this is the appropriate place to post it though. :)

        Yup. Wrong site, fella :-)
Re: Crossword helper
by Anonymous Monk on Jan 28, 2001 at 01:20 UTC
    When I run it, I get the following error:

    exists operator argument is not a HASH element at ./crossword.pl line 61.

    Any idea why?

    From perl -v:

    This is perl, version 5.005_03 built for i386-linux

      (From memory) Perl 5.6 extended the exists function to work on array elements and functions. Replace it with defined to get nearly the same meaning, close enough to the same to work for this script.

              - tye (but my friends call me "Tye")
        That did it.

        Thanks!

Re: Crossword helper
by chipmunk (Parson) on Jan 30, 2001 at 23:52 UTC
    I notice that you read all the words into memory, even though you only want words of a single length. You can save memory by filtering out the other words as you read:
    while (<INP>) { chomp; push @chk if length == $word_length; }
    On the other hand, you might like to make this an interactive script, which reads in all the words, and then prompts for commands from standard input in a loop.

      When I first wrote this, user input was read from STDIN within a loop. This way you could check multiple words, etc... That necesitated keeping it all in memory. Then I made it command line.

      But as you have pointed out... I didnt clean it up for processing it this way. I should modify as you suggest....

      THANKS!!
      zzSPECTREz

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://54732]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found