# In response to Eugene van der Pijll's finding of a bug in my code and a 
# potential resource hog, here is my solution corrected.  Unfortunately, 
# though I cannot figure out why at this point, I could not declare 
# %found_this_iteration with my and instead had to use local for it to work as 
# I had intended.  Can anyone explain that to me?  -Alan
# 

my (%goodones, %sequences);

open(DICT,$ARGV[0]) || die $!;
while (chomp($_ = <DICT>)) {
   local %found_this_iteration;
   /(\w{4})
    (?{
       unless ($found_this_iteration{lc $1}++) {
          $sequences{lc $1}++;
          $goodones{lc $1} = exists $goodones{lc $1} ? undef : $_;
       }
    })
    (?!)
   /gx;
}
close(DICT);

open(QUESTIONS,">questions") || die $!;
open(ANSWERS,  ">answers"  ) || die $!;

for (sort keys %sequences) {
   next unless $sequences{$_} == 1;
   print QUESTIONS "$_\n";
   print ANSWERS   "$goodones{$_}\n";
}

close(QUESTIONS);
close(ANSWERS);

