Here's one of my scripts from my perl drawer. It reads in a dictionary (standard: one word per line of a file), asks the user for a list of letters to unscramble, and does so, giving a list of *all* words 3 letters or longer found within the letters given. What I've wanted to do with this is create a boggle script: the user would input the game board and the script would output all the words found in the puzzle, giving the location of the first letter of each word to boot (for easy finding!). Have yet to get around to that, though it's not one of my priorities, so we'll see if I ever do it.
If you wanted to change this script to only unscramble the letters (ie: find only words that contain the same number of letters as the letters given), just take out the outer for() loop and change the next unless length $word == $len; line to next unless length $word == length $letters;. Of course, mine ends up being much longer in the end :(
#!/usr/bin/perl -w use strict; sub cls { system $^O =~ /win32/i ? 'cls' : 'clear' } sub load_dict { my $path = shift; cls(); print "Loading the dictionary..."; open my $dict, $path or die "Dictionary not found at '$path'\n"; chomp( my @words = <$dict> ); close $dict; print " Done.\n", scalar(@words), " words in the dictionary.\n";; sleep 3; return \@words; } my $path = $ARGV[0] or die qq{Usage: "perl }, $0=~m#.*[/\\](.*)$#, qq{ path_to_dictionary"\n}; my $words = load_dict( $path ); for (;;) { my @found; cls(); print 'Letters to unscramble: '; chomp( my $letters = <STDIN> ); last unless $letters; for my $len (reverse( 3 .. length $letters )) { my $s = join '?', (sort( split //, $letters ), ''); for my $word (@$words) { next unless length $word == $len; my $t = join '', sort (split //, $word); push @found, $word if $t =~ /^$s$/; } } print "\n", scalar(@found), " words found in '$letters':\n"; print " - $_\n" for @found; print "\nPress enter to start new scramble...\n"; <STDIN>; }
"User error. Replace user and press any key to continue."
In reply to Re: "Jumble" solver
by Coruscate
in thread "Jumble" solver
by ackohno
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |