I don't know if anyone else will like this, but I think its kindof neat. Its a nasty little way to unscramble the word puzzle in the comics of the news paper.
The way it unscrambles a word is this: first it makes a hash of every word in a word list (as value) and the key is the letters of the word in order (ex: iiiimppssss=> mississippi). then the scrambled word the user specifies is sorted the same way and used as the key in the hash.
to use first make the database with scriptname -m -w wordlist (-d database path). if you use your own database path you will also have to do -d path when you run: unscramble -u scrambledword (-d path)
#!/usr/bin/perl
use Getopt::Std;
getopts("u:mw:d:");
#defualt file locations over ride with option -d <path>
$wordlist = "/usr/share/lib/dict/words";
$database = "$ENV{HOME}/.word_db";
#option -u(nscramble) ...
if($opt_u){
#open default if not specified
open(IN, $opt_d ? "<$opt_d" : "<$database");
#build the lookup table
while(<IN>){
($value, $key) = split /:/;
chomp $key;
$hash{$key} = $value;
}
#arange the letters to match format of database
$sorted = join "",sort split//,$opt_u;
#display unscrambled word
print $hash{$sorted}."\n";
}
#build the database file, -m(ake)
elsif($opt_m){
open(IN, $opt_w ? "<$opt_w" : "<$wordlist");
open(OUT, $opt_d ? ">$opt_d" : ">$database");
#write lookup table, word:sorted letters
while(<IN>) {
chomp;
$sorted = join "",sort split//;
print OUT "$_:$sorted\n";
}
}
else {
print "Usage: ./$0 [-m] [-w[wordlist]] [-d [database]][-u scramble
+dword]\n";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.