I wrote c program that outputs all permutations of a word to a txt file.

ate
aet
tae
tea
eat
eta

I also have a txt file of all the words in the dictionary. I would like to take the first entry in permutations.txt and search dictionary.txt to see if its a valid word then the second entry ect...

I have a little experience using perl, but only for parsing one file. Would it be a good fit for what i'm trying to do. Anybody have some suggestions or point me in the right direction?

Recommended Answers

All 4 Replies

I wrote c program that outputs all permutations of a word to a txt file.

ate
aet
tae
tea
eat
eta

I also have a txt file of all the words in the dictionary. I would like to take the first entry in permutations.txt and search dictionary.txt to see if its a valid word then the second entry ect...

I have a little experience using perl, but only for parsing one file. Would it be a good fit for what i'm trying to do. Anybody have some suggestions or point me in the right direction?

You can do this easily in Perl. A hash in Perl works sort of like a dictionary in that it consists of unique keys associated with values, so read all the words from your dictionary file into memory as keys to a hash. Since you don't care about definitions of the words you don't need to associate any values with the keys in your hash, so make the values undef . Having all your dictionary words stored as keys in a hash allows you to test if any given word exists in the hash much more easily than having to look for it in the dictionary file. For an introduction to using hashes in Perl see http://www.perltutorial.org/perl-hash.aspx

All your sample data words are in lower case letters but, in case future data include mixed-case words, make sure to convert every word to either upper or lower case (choose one or the other but be consistent) as you read it into each variable (including words in your dictionary) so you won't make the mistake of searching for 'teA' in your hash which may have only an entry for 'Tea'.

For example, you could do it as follows:

#!/usr/bin/perl;
use strict;
use warnings;

my %dict;
my $dictfilename = 'words-english.txt';

open my $fh, '<', $dictfilename or die "Failed to open $dictfilename: $!";
while (my $word = <$fh>){
    chomp $word; #Remove end-of-line character
    my $upper_word = uc($word);#Convert to upper case
    $dict{$upper_word} = undef; #Add word as key to hash (no value associated)
}
close $fh;

my $permsfilename = 'permutations.txt';
open $fh, '<', $permsfilename or die "Failed to open $permsfilename: $!";
while (my $word = <$fh>){
    chomp $word; #Remove end-of-line character
    my $upper_word = uc($word);#Convert to upper case
    if (exists $dict{$upper_word}){
        print "Yes, $word is in the dictionary.\n";
    }
    else{
        print "No, $word is NOT in the dictionary.\n";
    }
}
close $fh;

(Today I learned that 'eta' is an English word naming the seventh letter of the Greek alphabet.)

Ah thank you very much! Works like a charm :)

You're welcome. Please don't forget to mark this thread 'solved'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.