import-bot (20211) [Avatar] Offline
#1
[Originally posted by graham patterson]

Hi Andrew,

Firstly, many thanks for the excellent book -- the precision and clarity of
the explanations is outstanding, and the practical value of starting off with
something that is actually useful like faqgrep (instead of endless noddy
programs like some books) is tremendous. OK, my first question:

In discussing the mathq program in chapter 3, on pages 35 and 48 you give two
examples of the output when an incorrect answer is entered, like this:

Incorrect: 21 / 7 = 3 # p35
Incorrect: 0 / 0 = 8 # p48

In fact, since the program as given on p44-45 is just printing the original
question, a space and then the solution, it prints this:

Incorrect: 21 / 7 = ? 3
Incorrect: 0 / 0 = ? 8

So we need to chop off the end of the question and replace it with the right
answer. Here's my solution, using substr (top of p45):

} else {
my $fixed_question = substr($question, 0, -2); # remove trailing ' ?'
print "Incorrect: $fixed_question $solution
";
}

which now prints the responses as shown on pp35 and 48. Given the nature of
Perl, I am sure there are many other ways to do the same thing, using
something other than substr. There's probably no "best" way, but what would be
the most Perlish way? What would a veteran Perl programmer do here?

Thanks for your time and for the superb book,
Graham Patterson
import-bot (20211) [Avatar] Offline
#2
Re: using substr in mathq program
[Originally posted by jandrew]

Hi Graham,

Hmm, quite surprising this discrepancy has gone unnoticed so long ---
and the spec on p35 also shows the questions indented one space as
well (and an exclamation point after 'Correct'). After this much
time, I am inclined to not call it an errata but rather an
undocumented exercise for the reader --- just as you seem to have
done smilie

As to your fix, it is simple, direct, and it works! Plus, although
a regex substitution solution might seem more "perlish" (ie, swap the
'?' with the $solution):

} else {
$question =~ s/?/$solution/;
print "Incorrect: $question
";
}

it is often good to stick with plain string functions like substr()
when they are all that's required!

Two other alternatives also present themselves: 1) go back and
rewrite the specs and example output while no one is looking so the
current code is actually correct --- too late for that I guess, or 2)
fix the problem closer to the source: ie, change the $question string
so it does not have the ' ?' in it at all (making sure we change both
places where the $question string is defined), and put the ' ?' in
the print statement that displays the question:

print "$question ?
";

That actually requires changes to 3 existing lines of code (rather
than the addition of new code) --- but these changes are tiny
(delete a couple characters from two lines, add a couple characters
to another line), and the problem disappears instead of needing
fixing.

What's the "best" way? Of the two "fixit" methods, I'd guess a number
of experienced Perl programmers would immediately think "regex" and
try some variation on the substitution method.

Personally, I think your substr() method is simpler and more efficient
(even if you hadn't said all those nice things about the book). And
if you modify the $question variable itself rather than creating
a new variable, it doesn't require any change to the original print
statement that followed:

} else {
$question = substr($question, 0, -2);
print "Incorrect: $question $solution
";
}

or even:

} else {
substr($question, -2) = ""; # a bit trickier
print "Incorrect: $question $solution
";
}

However, I'd be more inclined to say that making the problem go away
without introducing any new code would be the better overall method.

I hope you continue to enjoy the book and Perl!

andrew
import-bot (20211) [Avatar] Offline
#3
Re: using substr in mathq program
[Originally posted by graham patterson]

Hi Andrew,

Yes, your regex solution is exactly the sort of thing I was thinking of when I
said a more "Perlish" (perlish?) way of doing it -- it's short, to the point
and has the requisite cryptic punctuation! And in the substr version, of
course you are right about simply modifying the $question variable instead of
unnecessarily creating a new one. It's kinda hard trying to remember Perl
syntax *and* Occam's razor at the same time smilie

Thanks for your help,
Graham
import-bot (20211) [Avatar] Offline
#4
Re: using substr in mathq program
[Originally posted by jandrew]


It's kinda hard trying to remember Perl syntax *and* Occam's
razor at the same time smilie
-- Graham Patterson

Now that's the best candidate for admission to my quotes file that
I've seen in a long while --- I hope you don't mind?

regards
andrew
import-bot (20211) [Avatar] Offline
#5
Re: using substr in mathq program
[Originally posted by graham patterson]

Andrew,

Feel free to quote anything I say that makes sense! smilie

Regards, Graham