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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.