On Jun 1, 2004, at 1:07 PM, Mark Jason Dominus wrote:
(Pr. Gray's was about the same length, and was beautifully
straightforward, except for one line in the middle that gave me the
willies. But it's worth looking at.)
I warned that my Search-In-One-Variable-Replace-In-Another trick was
probably bad style. Apparently MJD agrees. <laughs>
Surprisingly, it seemed natural at the time and was the first thing I
thought of. I know, there's probably drugs for that and I'll look into
them.
I appreciate MJD's praise though, so I'll pay him back with a cleaner
solution. Replacing the scary:
$limit-- unless
$word =~ s/($letter)/substr $puzzle, pos($word), 1, $1; $1;/ige;
with the sane:
for my $i (0..(length($word) - 1)) {
my $test = substr $word, $i, 1;
substr $puzzle, $i, 1, $test if lc $letter eq lc $test;
}
$limit-- unless $word =~ m/$letter/i;
works just fine and I'll bet it gives MJD (and others) less "willies".
Just to be clear, the first method is bad because it's using a
construct for its side effects only. You can see this in the fact that
I have to tack on an extra $1 at the end of the replace, to keep from
mangling the original words with my trick.
I repent my wicked ways. Forgive me.
James
|