CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Word Unscrambler

 



cheechand
New User

Jun 8, 2008, 10:54 PM

Post #1 of 4 (992 views)
Word Unscrambler Can't Post

Hello, I am new to programming and am trying to write a program that reads in 10 scrambled words, unscrambles them, and prints them to the monitor. I borrowed code from a different unscrambling program and have been trying to modify it to suit my needs:

Code
#!/usr/local/bin/perl 

#Opens a file containing scrambled words and reads words into an array
$file = '/home/****/Perl/words';
open(INFO, $file);
@words = <INFO>;
close(INFO);

#"wordlist" is a dictionary
my $dict = '/home/****/Perl/wordlist';

#for every word in array
foreach $word(@words)
{
#enter characters into an array
my @chars = split("", $word);

$word = "";
@chars = sort (@chars); #sorts chars by alphabetical order
foreach (@chars) {
$word .= $_;
} #algorithm incomplete
$word =~ $dict;
print "$word,";
#Need to find a way to continue to the next word if the condition is met
}
print "\n";


My problems so far are that I believe the algorithm that scrambles the words in every possible order is incomplete and I cannot figure out how to move on to the next word if a dictionary match is found. Any help would be appreciated!

Thanks,
Cheech


KevinR
Veteran


Jun 9, 2008, 3:16 AM

Post #2 of 4 (981 views)
Re: [cheechand] Word Unscrambler [In reply to] Can't Post

use the List::Permutor module to find all the permutations of the scrambled words

http://search.cpan.org/~phoenix/List-Permutor-0.022/Permutor.pm



I leave it up to you to figure out how to use it. Your code has more problems though. Like this line is not doing anything at all:




Code
 $word =~ $dict; 



Needs to be wrapped in a conditional:




Code
   

if ($word eq $dict) }

print "Some cool message";

}



Is this school work?
-------------------------------------------------


meloyelo
User

Jun 9, 2008, 12:06 PM

Post #3 of 4 (976 views)
Re: [cheechand] Word Unscrambler [In reply to] Can't Post

This looks like homework, so I'll just give you this hint:

Would a hash like this be helpful to you?


Code
%hash = ( 
a => 'a'
abt => 'bat',
act => 'cat',
dgo => 'dog',
efl => 'elf',
'fgor' => 'frog',
...
);



cheechand
New User

Jun 11, 2008, 8:09 PM

Post #4 of 4 (967 views)
Re: [meloyelo] Word Unscrambler [In reply to] Can't Post

Hey all,

Just to clarify, this isn't a homework assignment

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives