public
Created

Find words that contain at least "msixpodual"

  • Download Gist
msixpodual-words.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Text::Table;
 
my @flags = split //, "msixpodual";
my %flags;
@flags{@flags} = ();
 
my %words;
 
while (my $word = <>) {
chomp $word;
my $cp = $word;
my $remaining = '';
 
my $score = 0;
for my $flag (@flags) {
if ($cp =~ s/$flag//i) {
$score++;
} else {
$remaining .= $flag;
}
}
 
# Skip non-serious candidates
next if length($cp) > 0;
 
if ($score) {
$words{$word} = {
score => $score,
remaining => $remaining,
}
}
}
 
my @order = sort {
($words{$b}->{score} <=> $words{$a}->{score}) or
length($a) <=> length($a)
} keys %words;
 
my @tr;
for my $word (@order) {
push @tr => [
$words{$word}->{score},
$word,
$words{$word}->{remaining},
];
}
 
my $tb = Text::Table->new(
"Score", "Word", "Remaining"
);
$tb->load(@tr);
print $tb;

Oops: length($a) <=> length($a)
(Not that it matters; if two words have the same score, then they have the same length as well. In fact, the score /is/ the length!)

There's a much improved version of this on perl5-porters now. This was just something I hacked up and is full of bugs and misfeatures :)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.