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:
wordlist unscramble

 



ascii
New User

Mar 23, 2011, 10:55 AM

Post #1 of 2 (1048 views)
wordlist unscramble Can't Post

okay so i have a crazy long list of words in wordlist.txt and i have words that have been randomly taken from this list and scrambled. these are stored in a scrambled.txt. i want to find what the sunscrambled words i have a program that will print out the matching word, but it only functions if the words in scrambled.txt are unscrambled. that is to say, if i had the word digital unscrambled in both files the program would make the match, but if i scrambled the letters of digital the program wouldnt function. i need some way to see if the letters match instead of if the whole words match. thanks!
source:

Code
 
#!/usr/local/bin/perl

open (INFO, 'wordlist.txt');
open (SCRM, 'scrambled.txt');
open (UNSCRM, '>>unscrcambled.txt');

@words = <INFO>;
@scrambled = <SCRM>;
chop (@scrambled);
chop (@words);

foreach $scrambled (@words) {
if ($scrambled eq @scrambled[0]) {
print UNSCRM "$scrambled,";
}
if ($scrambled eq @scrambled[1]) {
print UNSCRM "$scrambled,";
}
if ($scrambled eq @scrambled[2]) {
print UNSCRM "$scrambled,";
}
if ($scrambled eq @scrambled[3]) {
print UNSCRM "$scrambled,";
}
if ($scrambled eq @scrambled[4]) {
print UNSCRM "$scrambled,";
}
if ($scrambled eq @scrambled[5]) {
print UNSCRM "$scrambled,";
}
if ($scrambled eq @scrambled[6]) {
print UNSCRM "$scrambled,";
}
if ($scrambled eq @scrambled[7]) {
print UNSCRM "$scrambled,";
}
if ($scrambled eq @scrambled[8]) {
print UNSCRM "$scrambled,";
}
if ($scrambled eq @scrambled[9]) {
print UNSCRM "$scrambled,";
}
}

close (INFO);
close (SCRM);
close (UNSCRM);



(This post was edited by ascii on Mar 23, 2011, 10:56 AM)


miller
User

Mar 23, 2011, 11:34 AM

Post #2 of 2 (1041 views)
Re: [ascii] wordlist unscramble [In reply to] Can't Post

Just build a lookup hash


Code
use strict; 

# Build word list
my %words;
while (<DATA>) {
chomp;
my $key = join '', sort {$a cmp $b} split '';
$words{$key} = $_;
}

# Process scrambled words
for my $scrambled (qw(oof abc baza baz bz bzi ofo)) {
my $sorted = join '', sort {$a cmp $b} split '', $scrambled;
if ($words{$sorted}) {
print "$scrambled - matches $words{$sorted}\n";
} else {
print "$scrambled\n";
}
}

__DATA__
foo
bar
baz
biz


 
 


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

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