(Message qotw/d:1077)
/home/mjd/bin/mailpager 1077
Received: (qmail 18375 invoked by alias); 14 Dec 2002 12:09:16 -0000
Mailing-List: contact perl-qotw-discuss-help@plover.com; run by ezmlm
Precedence: bulk
List-Post: <mailto:perl-qotw-discuss@plover.com>
List-Help: <mailto:perl-qotw-discuss-help@plover.com>
List-Unsubscribe: <mailto:perl-qotw-discuss-unsubscribe@plover.com>
List-Subscribe: <mailto:perl-qotw-discuss-subscribe@plover.com>
List-Digest-Subscribe: <mailto:perl-qotw-discuss-digest-subscribe@plover.com>
List-URL: <URL:http://perl.plover.com/qotw/>
Delivered-To: mailing list perl-qotw-discuss@plover.com
Received: (qmail 18367 invoked from network); 14 Dec 2002 12:09:15 -0000
Date: Sat, 14 Dec 2002 13:07:01 +0100
To: perl-qotw-discuss@plover.com
From: Not_a_Number <djones@noos.fr>
Subject: Re: Perl Quiz-of-the-Week #7
Reply-To: djones@noos.fr
Organization: Vaudeloges Communications
X-Mailer: Opera 5.12 build 932
X-Priority: 3 (Normal)
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"

My personal decision for 'doesn't', 'anybody'd', 'close-up' etc:
treat 'oesn', 'anyb', 'lose' as valid strings
but not 'sn't', 'dy'd', 'se-u' (or 'esnt', 'odyd', 'seup')

Here it is (seemingly different from other solutions posted to date)

use strict;
use warnings;

my %strings;

while (<>) {
    chomp;
    next if length $_ < 4;
    for my $i (0..length($_) - 4) {
        my $substring = lc substr($_, $i, 4);
        $strings{$substring}++;
        $strings{$substring} .= ":$_";
    }
}

open Q_OUT, ">questions" or die "Cannot open questions: $!";
open A_OUT, ">answers" or die "Cannot open answers: $!";
for (sort keys %strings) {
    my ($count, $word) = split /:/, $strings{$_};
    next if $count > 1 or /[^a-zA-Z]/;
    print Q_OUT "$_\n";
    print A_OUT "$word\n";
}
close Q_OUT;
close A_OUT;

