#!/usr/local/bin/perl -lnw
# 
# Not really any different from anyone else's, but I used some syntax
# features that I don't normally use (namely "for" as a statement
# modifier), and I think the result looks pretty darn elegant.
# 

use constant SEQ_LEN => 4;
use strict;
use vars qw($Q $A %Case %X);

BEGIN {
    $Q = "questions";
    $A = "answers";
    while (@ARGV && $ARGV[0] =~ /^-/) {
	my $switch = shift @ARGV;
	($Q = shift @ARGV), next
	    if $switch eq "-q";
	($A = shift @ARGV), next
	    if $switch eq "-a";
	last
	    if $switch eq "--";
	warn "ignoring unrecognized option $switch\n";
    }
}

# handle repeated words and case-folding
my $word = lc($_);
next if exists $Case{$word};
$Case{$word} = $_;

# handle repeated substrings within word;
# exclude substrings that contain punctuation
my %seq = ();
++$seq{$_}
    for grep(!/[^a-z]/,
             map(substr($word, $_, SEQ_LEN),
                 0 .. length($word) - SEQ_LEN));

# note multiple occurrences of substring
$X{$_} = exists($X{$_}) ? undef : $word
    for keys %seq;

END {
    open Q, ">$Q" or die "cannot open questions file $Q: $!\n";
    open A, ">$A" or die "cannot open answers file $A: $!\n";

    my ($k, $v);
    defined($v) and print(Q $k), print(A $Case{$v})
        while ($k, $v) = each %X;
}
__END__

-- 
Seth Blumberg, a.k.a. Metal Fatigue, a.k.a. Nodens <sethb@pobox.com> O-
-  --  ---  -----  --------  -------------  --------  -----  ---  --  -
"I have long known that it is part of God's plan for me to spend a
 little time with each of the most stupid people on Earth...."
                                                         -- Bill Bryson
