#!/usr/bin/perl -w



sub suggestion
{
	my ($word) = shift;
	print "Other possibilities for $word\n";

	# Make pattern
	$pattern = "";
	for($i = 0; $i < length($word); $i++)
	{

		# one way to do this
		#$first = substr($word, 0, $i);
		#$rest = substr($word, $i+1, 9999);
		#print "First = $first Rest = $rest\n";
		#$pattern .= "^$first.$rest\$|";

		# another shorter way to do this
		#$tmp = $word;
		#substr($tmp, $i,1) = ".";
		#$pattern .= "^$tmp\$|";

		# another way to do this
		$tmp = $word;
		$tmp =~ s/(.{$i}).(.*)/$1\.$2/;
		$pattern .= "^$tmp\$|";
	}
	# Finish pattern .. eliminate trailing "|";
	chop($pattern);
	print "Final pattern $pattern\n";

	# look for words that match
	foreach $possible (keys %words)
	{
		print "Suggestion:\t$possible\n" if ( $possible =~ $pattern);
	}

	# One line way to do the loop above
	#print map { "Suggestion:\t" . $_ . "\n" } grep { $_ =~ $pattern } %words;
}

# First load up the dictionary
open(DICT, "</usr/share/dict/words") || die "Unable to open dict";
while( $word = <DICT> ) {
	chomp($word);
	$words{$word} = 1;
}

# Get words to look up
print "Enter a word to look up\n";
while ($word = <STDIN>) {
	chomp($word);
	if (defined($words{$word})) {
		print "Found\n";
	}
	else {
		print "Not Found\n";
		&suggestion($word);
	}
	print "Enter a word to look up\n";
}

