I am very sure the code below can be simplified. The exercise was to take a word list, rearrange each word's letter to be in alpha order (e.g. barter = aberrt). The rearranged letter would be the keys and the word be be assigned to that key. Similar keys could then have multiple words assigned to it. I've written below to go as far as splitting, alphabetizing and then creating a list of the rearranged word with the original word separated by a comma on each line I did this in steps to see what was happening at each while (learning Perl).
I was trying to create the hash in the 3rd while loop with something like this:
($temp1, $temp2) = split (/,/, $_);
$words[$temp1]= $temp2
Also, I kept count in the original loop and the last loop to make sure I ended up with the same number of words. For some reason, my 3rd loop's output never completes. I am missing words. I am guessing this is a memory issue.
#!/~/gdevars/perl -w
%words = ();
$windex = 0;
$i = 0;
$back = 0;
@alpha =();
@anagrams = ();
@keys = ();
$temp1 = 0;
$temp2 = 0;
open (LIST, '2of12inf.txt') or die "Can't open OUT file: $!\n";
open (OUTPUT2, '>Dictionary2.txt') or die "Can't open OUT file: $!\n";
open (LIST2, 'Dictionary2.txt') or die "Can't open OUT file: $!\n";
open (OUTPUT3, '>Dictionary3.txt') or die "Can't open OUT file: $!\n";
open (LIST3, 'Dictionary3.txt') or die "Can't open OUT file: $!\n";
open (OUTPUT4, '>Anagram.txt') or die "Can't open OUT file: $!\n";
while (<LIST>) {
chomp;
my @alpha = split(//,$_);
my @alpha = sort @alpha;
print OUTPUT2 "$i: @alpha : $_\n";
$i++;
}
while (<LIST2>) {
chomp;
s/\s+//g;
print OUTPUT3 "$_\n";
}
while (<LIST3>) {
chomp;
s/\s+/,/g;
s/\n/,/g;
s/:/,/g;
print OUTPUT4 "$_\n";
$i++;
}
close LIST;
close LIST2;
close LIST3;
close OUTPUT2;
close OUTPUT3;
close OUTPUT4;
Thanks to anyone for looking at this-