Forum archive 2000-2006

Mark Schmitt - Debugging Perl References

Mark Schmitt - Debugging Perl References

by Arnold Pizer -
Number of replies: 0
inactiveTopicDebugging Perl References topic started 3/21/2002; 7:12:05 AM
last post 3/21/2002; 10:09:20 AM
userMark Schmitt - Debugging Perl References  blueArrow
3/21/2002; 7:12:05 AM (reads: 984, responses: 3)
Since I teach Algebra 2, I often need to be able to multiply polynomials together. Out of haste, I hard-wired a bunch of problems to use arrays to do this. I've since taken a step back, and tried to code a function that would accept two arrays (the coefficients of the polynomials) and return an array (the product of the polynomials). Unfortunately, I've run into some problems I can't debug. I was hoping someone here might be able to tell me what is going wrong and how to fix it. The offending snippet of code, and WeBWorK's error message are below. Thanks in advance.

Mark

ERROR caught by PGtranslator while processing problem file:../../Problems/Math/Complex/PolyTest.pg
*
Not an ARRAY reference at (eval 53) line 23.



*



------Input Read
1##DESCRIPTION
2## Algebra problem: complex numbers
3##ENDDESCRIPTION
4
5##KEYWORDS('algebra', 'complex number')
6
7DOCUMENT(); # This should be the first executable line in the problem.
8
9
10loadMacros(
11PG.pl,
12PGbasicmacros.pl,
13PGchoicemacros.pl,
14PGanswermacros.pl,
15PGauxiliaryFunctions.pl,
16PGcomplexmacros.pl);
17
18TEXT(&beginproblem);
19$showPartialCorrectAnswers = 1;
20
21sub PolyMult {
22my ($xref,$yref) = @_;
23@local_x = @{$xref};
24@local_y = @{$yref};
25foreach $i (0..$#local_x+$#local_y){$result[$i]=0;}
26foreach $i (0..$#local_x) {
27foreach $j (0..$#local_y){
28$result[$i+$j] = $result[$i+$j]+($local_x[$i]*$local_y[$j]);
29}
30}
31return @result;
32}
33
34
35
36@com1=(1,3,5);
37@real=(1,2,-3);
38
39
40@prod= PolyMult(\@com1,\@real);
41
42





<| Post or View Comments |>


userGavin LaRose - Re: Debugging Perl References  blueArrow
3/21/2002; 7:55:38 AM (reads: 1152, responses: 0)
Could it be that in a problem file you need to code backslashes ("\") with tildes ("~~")?

Thus,

  @prod = PolyMult(~~@com1, ~~@real);

 

Hope this helps,
Gavin

<| Post or View Comments |>


userMark Schmitt - Re: Debugging Perl References  blueArrow
3/21/2002; 8:23:33 AM (reads: 1156, responses: 0)
That did the trick. Thanks Gavin.

Mark

<| Post or View Comments |>


userMichael Gage - Re: Debugging Perl References  blueArrow
3/21/2002; 10:09:20 AM (reads: 1158, responses: 0)
Mark,

One caution. If or when you move this subroutine to a macro file (e.g myPGmacros.pl) you will need to replace the ~~ by \ again.

The thinking is as follows.

In the problem files (.pg files) TeX is used more often than the fancier forms of perl, hence all of the backslashes are protected in favor of TeX and you need to use ~~ in order to obtain a perl backslash.

For subroutines (the ones located in the .pl macro files) it seemed that TeX was not used extensively, but perl was. In these files all TeX commands need to have double backslashes: e.g.

"\\alpha""

but the perl backslashes work in the normal perl fashion. ~~ has no meaning in these files.

Hope this helps.

This gotcha is one that I run into fairly frequently, but I haven't figured out any clever way to avoid it.

Take care,

Mike

<| Post or View Comments |>