Skip to content

Instantly share code, notes, and snippets.

@thunderpoot
Last active March 2, 2025 12:45
Show Gist options
  • Save thunderpoot/70cfbc3d1a4aafa801ea15b0f8d98a7d to your computer and use it in GitHub Desktop.
Save thunderpoot/70cfbc3d1a4aafa801ea15b0f8d98a7d to your computer and use it in GitHub Desktop.
Simple Perl script to find words containing only letters provided as argument
#!/usr/bin/env perl
# This script is useful when proofing with only some glyphs completed…
# Usage example:
# $ perl findwords.pl qwertyasdf
# Searching for words in /usr/share/dict/words containing only q, w, e, r, t, y, a, s, d, f
# westerwards
# afterstate
# aftertaste
# afterwards
# ...
use strict;
use warnings;
sub println
{
my ( @args ) = @_;
print join( '', @args ) . "\n" or exit 1;
return;
}
sub uniq
{
my %seen;
return grep { ! $seen{ $_ }++ } @_;
}
my @all;
push @all, split '', $_ for ( @ARGV );
my @results;
my @letters = uniq( @all );
my %hash = map { $_ => 1 } @letters;
my $path = '/usr/share/dict/words';
open my $dict, $path or die( "Could not open $path: $!" );
println( "Searching for words in \e[1m$path\e[m containing only ", ( join ', ', @letters ) =~ s/, $//r );
while( my $line = <$dict> )
{
my $skip;
chomp $line;
for ( split '', $line ) { unless ( $hash{ $_ } ) { $skip = 1; last; } }
next if $skip;
push @results, $line;
}
close $dict;
@results = ( sort { length $b <=> length $a } @results );
print "$_\n" for @results;
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment