Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with scanning a huge text file 3

Status
Not open for further replies.

jackz15

Programmer
Jun 28, 2006
103
US
hi i have to write a perl program which takes in a word from the user and then returns all the possible anagrams from it(anagrams are just the words formed by scrambling up the current word). It has to check that the anagrams actually exist in this 5mb text file, or web page containing it. Then it prints out the new rods. I'm thinking of using LWP:simple to read right off the web page, but the problem is performance, wouldn't it be extremely laggy to read through the page everytime the uer enter a new word? Is there a way to increase the performance?
thanks in advance
 
I think this is more like what you are wanting to do:

Code:
#!\usr\bin\perl -w
use strict;
use List::Permutor;

my $word;
my @anagrams = ();
my @matches = ();

print "Enter a word: \n";
chomp($word = <STDIN>);
if ($word =~ /\W/) {
   print "Enter a word using only letters a-z: \n";
   chomp($word = <STDIN>);
}
$word = lc($word);

my $perm = new List::Permutor split(//,$word);
{
   local $" = '';
   while (my @set = $perm->next) {
      push @anagrams,"@set";
   }
}

my %anagrams = map {$_,1} @anagrams;

open (FILE, "<E://words.txt") or die "Can't open word.txt: $!";
while(<FILE>){
   chomp;
   push(@matches,$_) if (exists $anagrams{lc($_)});
}
close(FILE);

print 'Found ', scalar @matches, " anagrams of $word\n";
print "$_\n" for @matches;

You may want to limit the length of the word the user can enter. A 10 letter word has over 3.5 million permutations.
 
wow i totally forgot about map! your code is so simplified compared to mine.... But i don't think it is neccesary for me to have the:
Code:
if ($word =~ /\W/) {
...
}[\code]
cause there are also numbers and surprisngly slashes in the dictionary. and i also tested words ten letters long, it took like 15 seconds, so it seems that the speed is pretty nice. thanks for the help guys! i appreciate it!
 
The /\W/ (non word characters) was just a thought. You don't need it, but user input should always be validated as a practice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top