Did I understand points 1) and 2) correctly? This script finishes a list of almost 640,000 entries in less then a minute (50sec), adding conditions 3) and 4) should be really easy.
#!/usr/bin/perl
use feature 'say';
use warnings;
use strict;
my $file = '/etc/dictionaries-common/words';
open my $IN, '<', $file or die "$!";
my %words;
while (my $word = <$IN>) {
chomp $word;
undef $words{$word};
}
for my $word (keys %words) {
my $length = length $word;
my %found; # report each occurence just once
for my $pos (0 .. $length - 1) {
my $skip_itself = ! $pos;
for my $len (1 .. $length - $pos - $skip_itself) {
my $subword = substr($word, $pos, $len);
next if exists $found{$subword};
if (exists $words{$subword}) {
say "$subword in $word";
undef $found{$subword};
}
}
}
}
Update: if I ommit the "just once" condition, the script finishes in 40 secs on my Mac Mini.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|