I'm sorry to be back with yet another "minor" problem, but this is slowly driving me up the wall.
Even more so because I had this working but for some reason it doesn't now. The problem is with the regex wildcard ".".
#!/usr/bin/perl
#########################################
# The purpose of this program is to help
# cheat at Scrabble. Ultimately though it's more
# about the intellectual exercise of writing the
# actual program. I've tried using it (on http://isc.ro)
# and found that it's more work than it's worth. In fact,
# once I tried it (I *nearly* (*cough*) always informed
# my opponents beforehand) my rating quickly deteriorated.
#
# Also, I don't have the program on my phone so when I'm
# out and about I show my true colors anyway.
#
# There's actual a little embarrassing anecdote here:
#
# I started a game (20 minutes, challenge: DOUBLE) at
# home, full of confidence that I would beat my opponent
# with the help of my litle program. Unfortunately, my
# opponent wanted to adjourn due to some emergency. What
# happened then was ofcourse that we continued the game
# while I was driving a bus to work. Needless to say my
# performance was noticeable sub-stellar. So bad, in fact,
# that I had to come clean. Luckily for me he/she took it in
# full stride enjoying serving me my head on a platter. :)
#
# sbrothy 21:26 9/9/2021
#
#########################################
use autodie;
use diagnostics;
use strict;
use warnings;
use 5.010;
use File::Fetch;
my $ff = File::Fetch->new(uri =>
'https://www.wordgamedictionary.com/sowpods/download/sowpods.txt'
);
my $csw = $ff->fetch(to => '/tmp');
my @words;
open my $fh, '<', $csw;
foreach (<$fh>) {
tr/\r\n//d;
tr/\[A-Z]/[a-z]/;
next if /\s+/; # dirty way of removing comments
push @words, $_;
}
close $fh;
############################################
my $tiles = 'a.';
print "MATCHES FOR: $tiles\n";
my @m = find_matches($tiles);
print "\n@m\n";
############################################
$tiles = 's.';
print "MATCHES FOR: $tiles\n";
@m = find_matches($tiles);
print "\n@m\n";
############################################
sub find_matches {
my $letters = shift;
my $regex = join '', map "$_?", sort split //, $letters;
my @matches;
foreach my $word (@words) {
if(join('', sort split //, $word) =~ /^$regex$/) {
push @matches, $word;
}
}
return @matches;
}
As you can see the wildcard behaves erratically. I had this working but I'm pretty sure I didn't do anything special.
Will you humor me one last time?
Regards, sbrothy
Sorry, forgot to provide the output:
MATCHES FOR: a.
aa
MATCHES FOR: s.
as es is os sh si so
And just for the record changing the order of the letters doesn't change anything as they're sorted. Obviously.
-
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.