COP3353 - 2013 Spring
Assignment 3 "Solving Anagrams"
QUICK START GUIDE

First, please do not use the pack() function. I have seen numerous people trying to use it, and it's not suitable for this assignment. If your program already works using pack(), that's not a problem, but otherwise, I would suggest just simply removing it from your program.

(If you really want to learn about pack() --- and it's one of the most obscure features of Perl --- you can do so with "man perlpacktut", but it's dense reading. I will gladly assert that you won't learn anything useful for this assignment, though, if you do so. If you are trying to use the advice from The Perl Cookbook, it's useful enough information --- indeed, the recommended code below uses exactly the same split()/join() technique --- but the bits about pack() aren't relevant to this assignment.)

Here's the basic structure that I recommend for your ./build.pl program:

#!/usr/bin/perl -w

use strict;

# hash for your dictionary
my %dictionary;

# loop over input using the "diamond" operator
while(<>)
{
    # clean up any end-of-line issues
    chomp;

    # save the original word for later user
    my $word = $_;

    # use "split" to break word into individual letters stored in an array
    my @letters = split(//,$word);

    # sort the letters
    ...

    # put the letters back together with "join"
    my $key = join('',@letters);

    # now remove any excess characters
    # hint,hint,hint: try the s/// command!
    ...

    # now push all of the characters to lower-case with the lc() function
    ...

    # now check to see if this key is already in the dictionary
    if(exists($dictionary{$key}))
    {
        # yes, just append this answer to that key then...
        $dictionary{$key} = $dictionary{$key} . ",$word";
    }
    else
    {
        # no, create an entry with this word in it...
        $dictionary{$key} = "$word";
    }
    
}

# now output the dictionary
foreach(sort(keys(%dictionary)))
{
    print "$_:$dictionary{$_}\n";
}

For search.pl, while you could load the dictionary up with open(), it's actually much easier to call the external program egrep (this is not the internal grep() function that Perl has, but instead the one program in /bin/egrep.)
#!/usr/bin/perl -w 

use strict;

# make sure that the user provided a jumbled word to look up, and
# store it in $word
my $word;
if(defined($ARGV[0]))
{
    $word = $ARGV[0];
}
else
{
    print "Please provide a scrambled word\n";
    exit(0);
}


# as in build.pl, use split()/sort()/join() to find the canonical representation

# use "split()" to break word into individual letters stored in an array
my @letters = split(//,$word);

# sort the letters with sort()
...

# put the letters back together with "join()"
my $key = join('',@letters);


# make it lowercase with "lc()"

# finally, search for it externally using egrep
my $entry = `egrep "^$key:" dict.txt`;
chomp($entry);

# finally, split the $entry with "split()" and write your
# results
...
print "The possible anagrams for \"$word\" are \"$possibilities\"\n";