Re: simple wildcard regex problem.



I'm sorry, but there is no excuse for a goto "loop", in any reasonably
high-level language. This is basic programming, and has nothing to do
with Perl.

Yes I know but I don't know what else to use.

If you're looking for constructive criticisms of your code, by all
means, post away. But don't post again because you think you'll get a
different solution than I already offered - at the very least not
without trying that solution first.

Yes I am and I did try your solution but it didn't work. Here's the
code:

#!/usr/bin/perl
use strict; use warnings;

my $unsolvedword;
my $numberofletters;
my $wordlistfile1 = "/usr/share/dict/words";
my $wordlistfile2 = "enable2k_wordlist";
my $dict = $wordlistfile1;
my @guesswords;
my ($foo1, $foo2, $foo3);
my $solvedletters1;
my $solvedletters2;
my $regex;
my $regexstring;

print "\n #######################################";
print "\n Before you start solve for the letter e";
print "\n #######################################";
print "\n";
print "\n What's the word?, enter dashes ( - ) for unknown letters: ";
$unsolvedword = <STDIN>; chomp($unsolvedword); $numberofletters =
($unsolvedword =~ tr/a-z|-/a-z|-/);

print " The word is: \"$unsolvedword\", it has $numberofletters
letters.\n Is this correct? hit n to bail out, otherwise just hit
enter: ";
$_ = <STDIN>; chomp($_);

if ($_ eq "n"){
print "\n Goodbye.\n";
exit;
};

$solvedletters1 = "e"; #comment this out to run bad code.
WORKLOOP1:
$foo1 = "0";
#This block of code sets $regexstring:
if ($solvedletters1 eq "") {
while ($foo1 < $numberofletters) {
#bad code in here
$foo2 = substr($unsolvedword, $foo1, 1);
if ($foo2 eq "-"){
$regexstring .= "."; #<-- this here.
} else {
$regexstring .= $foo2;
}
$foo1++;
}
}
else {
$solvedletters2 = "[^$solvedletters1]";
while ($foo1 < $numberofletters) {
$foo2 = substr($unsolvedword, $foo1, 1);
if ($foo2 eq "-"){
$regexstring .= $solvedletters2;
} else {
$regexstring .= $foo2;
}
$foo1++;
}
}

#For debugging
print "\n<debug>\nRegex String: $regexstring\n</debug>\n";

print "\n Here are the results from your query:\n\n";

#Open wordlist file.
open(DATA, "< $dict")
or die "Couldn't open $dict for reading: $!\n";

$regex = qr{^$regexstring$};
my %freq; #keys = characters, values = frequency count.
while (<DATA>) {
chomp;
if (/$regex/) {
#Move matched words into a list for later use.
push @guesswords, "\n$_";
#Delete letters we already solved for.
$_ =~ s/[$solvedletters1]//g;
#Move letters into array for sorting.
my @chars = split("", $_);
$_ = ""; @chars = sort (@chars);
#Move (sorted) letters back into a string.
foreach (@chars) {
$foo3 .= $_;
}
$_ = $foo3; $foo3 = "";
#Remove duplicate letters, so they don't get counted.
$_ =~ tr///cs;
#print "$_\n"; #For debugging.
#Move processed letters into a hash for counting.
$freq{$_}++ for split(//, lc $_);
}
}

#Matched words
$_ = scalar @guesswords;
print " Guesswords in list: $_\n";
if (@guesswords <= "20") {
print "@guesswords\n\n";
}

#Print probabilities list, sort by hash value in ascending order
foreach $_ (sort {$freq{$a} <=> $freq{$b}} (keys(%freq))) {
print " $_\t=>\t$freq{$_}\n";
}

#For debugging
print "\n<debug>\nRegex: $regex\n</debug>\n";

$regex = ""; $regexstring = ""; ($foo1, $foo2, $foo3) = ""; undef
%freq; undef @guesswords;

#Round two
print "\n Continue solving word? hit n to bail out, otherwise just hit
enter: "; $_ = <STDIN>; chomp($_);
if ($_ eq "n") {
print "\n Goodbye.\n";
exit;
}

print " What letter did you try? "; $solvedletters1 .= <STDIN>;
chomp($solvedletters1);
print " What's the word now?, enter dashes for unknown letters: ";
$unsolvedword = <STDIN>; chomp($unsolvedword);
goto WORKLOOP1 #I'm new at programming so don't kill me!

.



Relevant Pages

  • Re: simple wildcard regex problem.
    ... This is basic programming, ... Do you only want to get a count of those characters (letters, ... hit n to bail out, ... push @guesswords, $_; ...
    (perl.beginners)
  • Re: enabling address books
    ... I can open a New Message, type in 3-4 letters of a contact's name, hit ... but Address Book seems to think it is empty? ... Wwhen I type in 3-4 letters of a contact's name in the 'TO:' box, Outlook fills in s the contact's email address. ...
    (microsoft.public.outlook)
  • Re: Too many functions?
    ... You have hit the limit of 7 nested functions ... One name and the first two letters are Ch or Sh ... Any ideas why I can't fix this one glitch? ...
    (microsoft.public.excel.misc)
  • Re: enabling address books
    ... j lunis wrote: ... I can open a New Message, type in 3-4 letters of a contact's name, hit ... Brian Tillman [MVP-Outlook] ...
    (microsoft.public.outlook)