#!/usr/bin/perl

#----------------------------------------#
#   Color Text: Perl Cookbook, pg 522 	 #
#----------------------------------------#
use Term::ANSIColor;

# Clear the screen with the system call `clear`
system (clear);

print 	color("red"),	"###############################################################",color("reset"),"\n", 
	color("white"),	"Presenting:                                                    ",color("reset"),"\n",
	color("white"),	"       Exersize 4.12  Problem 2                                ",color("reset"),"\n",
	color("red"),	"###############################################################",color("reset"),"\n",
	color("white"),	"Generate a master word file: \`aspell dump master \>dictionary.txt",color("reset"),"\n",
	color("white"),	"1  Read in dictionary file.                       ",color("reset"),"\n",
	color("white"),	"2  Sort words by word length         ",color("reset"),"\n",
	color("white"),	"3  Write words of same length to the same file. ",color("reset"),"\n";

# Open the dictionary file or bust.
open (DICTIONARY, "<dictionary.txt" ) || die "Could not open dictionary.txt:\t$!\n";

# evaluate each line in the file 
while (<DICTIONARY>){
	# assign the word to a variable cause $_ is not working
	$word="$_"; 
	chomp ($word);
	
	# determin the word length
	my $length=length($word);
	
	# open, append, and close, the word to the approprate file.
	open (WORDFILE, ">>chars\_$length\.txt" ) || die "Could not open chars\_$length\.txt:\t$!\n";
	print WORDFILE "$word\n";
	close (WORDFILE) || die "Could not close the file chars\_$length\.txt:\t$!\n";
}
close 	(DICTIONARY) || die "Could not close dictionary.txt:\t$!\n";
print color("green"),	"ALL DONE! List the directory",color("reset"),"\n";
