use strict; use warnings; $| = 1; print "Enter word: "; chomp(my $word = ); my @pairs = $word =~ /(?=(..))/g; my $matcher = qr/(?=(@{[join "|", @pairs]}))/; my %coef; open my $dict, "<", "/usr/share/dict/words" or die "Couldn't open dictionary: $!"; while (my $dictword = <$dict>) { chomp($dictword); # skip proper nouns and anything with a non-letter next if $dictword =~ /[^a-z]/; my $matches = () = $dictword =~ /$matcher/g; my $coef = 2 * $matches / (@pairs + length($dictword)-1); push @{$coef{$coef}}, $dictword; } print "Top coefficients for $word:\n"; for my $coef ((sort { $b <=> $a } keys %coef)[0..4]) { next if ! $coef; print "$coef: ", join " ", @{$coef{$coef}}, "\n"; }