http://www.perlmonks.org?node_id=797113


in reply to Re: Tk cryptogram (no, not cryptography)
in thread Tk cryptogram (no, not cryptography)

Thank you so much...

You're so welcome! I'm glad to see someone having fun with this. Do you happen to have a source of cryptograms in electronic form, such that you can extract input files for use with your version of the script? Or are you just using a text editor to create your input files? (I would have used my version more often if I didn't have to always type in the puzzle from the newspaper or puzzle book.)

If you're going to insist on having a puzzle file provided as a command-line arg, you can leave out the "Read File" button -- just call your "readFile" sub before you call MainLoop.

As for loading (a big or huge version of) a "dictionary" file that has been crafted in some strange and mysterious way, that seems like too much work -- there must be a way to just use a simple word list. I would point out that your format doesn't really need the initial "string length" field, since this is evident from the string itself.

I don't really understand what's going on with the patterns and the dictionary, but alas, I feel like you ought to be scolded about this part of your "doword" sub:

... my @hits = (); ... for ($i = 0; $i < $len; $i++) { if (substr($doword,$i,1) ne "-") { substr($pat,$i,1) = "="; } else { substr($pat,$i,1) = '-'; } if (substr($doword,$i,1) eq ":") { substr($pat,$i,1) = substr($ +doword,$i,1); } if (substr($doword,$i,1) eq "'") { substr($pat,$i,1) = substr($ +doword,$i,1); } if (substr($doword,$i,1) eq ",") { substr($pat,$i,1) = substr($ +doword,$i,1); } if (substr($doword,$i,1) eq ".") { substr($pat,$i,1) = substr($ +doword,$i,1); } if (substr($doword,$i,1) eq "!") { substr($pat,$i,1) = substr($ +doword,$i,1); } if (substr($doword,$i,1) eq "?") { substr($pat,$i,1) = substr($ +doword,$i,1); } if (substr($doword,$i,1) eq "\"") {substr($pat,$i,1) = substr($ +doword,$i,1); } if (substr($doword,$i,1) eq "_") { substr($pat,$i,1) = substr($ +doword,$i,1); } for ($j = 0; $j < 26; $j++) { if (substr($doword,$i,1) eq $letters[$j]) { $hits[$j]++; } } } # (end of first big "for loop" for ($j = 0; $j < 52; $j++) { if (defined $hits[$j]) { if ($hits[$j] > 1) { $got_a_pattern = 1; } } } # (end of this little "for loop" if ($got_a_pattern) { ...
Apart from those comments being a waste, I think all those calls to substr could be replaced with something like this:
... for my $chr ( split //, $doword ) { $pat .= ( $chr =~ /\W/ ) ? $chr : '='; for my $ltr ( @letters ) { $got_a_pattern++ if ( $chr eq $ltr ); } } if ($got_a_pattern) { ...
One other nit-pick, in the "extractTemplates" sub:
foreach (@template2) { $_ =~ tr/ //d; if ($_ eq " ") { next; } push @template,$_; }
Notice how the "if" condition there can never return true, because all the spaces in the string have been removed by the previous "tr///"; meanwhile, you are able to push empty strings onto your @template array. Maybe you want it to be like this?
tr/ //d; push @template, $_ if ($_); # true when $_ is not an empty s +tring
But I don't know what difference it makes in how things actually work. I noticed that if you hit the enter key more than once, you get extra spacing in some of the strings being displayed (but fixing that "if" condition didn't change this behavior). Anyway, good luck with that.