Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #
- # Bulk of the load credited to tybalt89 on irc.perl.org
- #
- use strict;
- use Getopt::Std;
- my @words;
- my $given;
- my @matches;
- my %sizes;
- my %opt;
- my $wordlist = "/usr/share/dict/words";
- getopts('c:s:e:w:', \%opt);
- if (!$opt{'c'}) {
- print "Usage: $0 -c <chars> [-s <start chars>] [-e <end chars>] [-w <path to wordlist>]\n";
- exit;
- }
- if ($opt{'w'}) {
- $wordlist = $opt{'w'};
- }
- open(my $WL_FP, $wordlist) || die "Could not open wordlist \"$wordlist\": $!\n";
- @words = <$WL_FP>;
- close($WL_FP);
- map (s/[\r\n]$//, @words);
- $given = $opt{'c'};
- if ($opt{'s'}) { $given .= $opt{'s'}; }
- if ($opt{'e'}) { $given .= $opt{'e'}; }
- foreach my $word (@words) {
- $given = join '', sort split //, $given;
- my $pat = join '.*', sort split //, $word;
- if ($given =~ $pat) {
- if ($opt{'s'} && $word !~ /^$opt{'s'}/) { next; }
- if ($opt{'e'} && $word !~ /$opt{'e'}$/) { next; }
- my $word_len = length($word);
- if ($word_len == 1) { next; }
- if (!defined $sizes{$word_len}) {
- my @tmp;
- $sizes{$word_len} = \@tmp;
- }
- push @{$sizes{$word_len}}, $word;
- }
- }
- foreach my $size (sort keys %sizes) {
- print "$size: " . join(', ',@{$sizes{$size}}) . "\n" ;
- }
Add Comment
Please, Sign In to add comment