## WeBWorK Problems

### unionTables

by Kenneth Appel -
Number of replies: 2
Davide,
I thought that I had understood how to use unionTables, but seem to be
wrong. What I have here seems essentially the same as something that worked
in another problem, but either I have misunderstood where I can use the
tables or I have made one of my (sad to say) usual errors.

Here is the code
#DESCRIPTION
##Type of
#ENDDESCRIPTION

DOCUMENT();
"PGstandard.pl",
"PGchoicemacros.pl",
"PGgraphmacros.pl",
"MathObjects.pl",
"compoundProblem.pl",
"contextCurrency.pl",
"contextInequalities.pl",
"unionTables.pl",
"unionLists.pl",
"unionMacros.pl",
"contextTF.pl",
);
#for currency use context("Currency") then currency($A); Context()->texStrings; TEXT(beginproblem());$showPartialCorrectAnswers = 1;

$a1=random(10,19,1)/10;$a2=random(20,29,1)/10;
$a3=random(30,39,1)/10;$a4=random(40,49,1)/10;
$ans1=String("T");$b1=random(10,19,1)/10;
$b2=random(20,29,1)/10;$b3=$b2;$b4=random(40,49,1)/10;
$ans2=String("T");$c1=random(10,19,1)/10;
$c2=random(20,29,1)/10;$c3=random(30,39,1)/10;
$c4=random(40,49,1)/10;$ans1=String("F");

$d1=random(10,19,1)/10;$ans1=String("T");

Context()->texStrings;
BEGIN_TEXT
$PAR For each of the following relation tables answer T if the relation is a function and F if it is not.$PAR
\{BeginTable().
AlignedRow ( [, 1, $a1, , 1,$b4, ,1, $c1, ,1 ,$d1],separation=>30,align=>"ri
ght").
AlignedRow ( [, 2, $a2, , 2,$b2, ,2, $c2, ,2 ,$d2],separation=>30,align=>"ri
ght").
AlignedRow ( [, 3, $a3, , 3,$b3, ,2, $c3, ,1 ,$d2],separation=>30,align=>"ri
ght").
AlignedRow ( [, 4, $a4, , 4,$b1, ,4, $c4, ,3 ,$d2],separation=>30,align=>"ri
ght").
AlignedRow([, ans_rule(2), , ans_rule(2),, ans_rule(2),,
ans_rule(2)],align=>"right")
.
EndTable() \}

END_TEXT
Context("TF");

ANS(ans1->cmp); ENDDOCUMENT() The display looks like this; For each of the following relation tables answer T if the relation is a function and F if it is not. \{ BeginTable(). AlignedRow ( [, 1, 1.9, , 1, 4.9, ,1, 1.1, ,1 ,1.7],separation=>30,align=>"right"). AlignedRow ( [, 2, 2.6, , 2, 2.7, ,2, 2.6, ,2 ,2.3],separation=>30,align=>"right"). AlignedRow ( [, 3, 3, , 3, 2.7, ,2, 3.2, ,1 ,2.3],separation=>30,align=>"right"). AlignedRow ( [, 4, 4.8, , 4, 1.7, ,4, 4.8, ,3 ,2.3],separation=>30,align=>"right"). AlignedRow([, ans_rule(2), , ans_rule(2),, ans_rule(2),, ans_rule(2)],align=>"right") . EndTable() \} Ken In reply to Kenneth Appel ### Re: unionTables by Davide Cervone - The problem is with the blank entries in the row arrays. Perl complains about the '[,' since you can't start an array with an empty entry. It also turns out that the empty entries within the array are ignored, so [1,,2,,3] is the same as [1,2,3]. If you really want blank entries, you need to use "" to do it, as in [1,"",2,"",3]. There are also a couple of other mistakes: First, in  Context("TF"); ANS(ans1->cmp);

the change of context has no effect. MathObjects retain the context in which they were created, so since \$ans1 is already a MathObject, it already knows its context.

Second, you should explicitly set the TF context before you create the T/F answers. It turns out that since you have loaded contextTF.pl as the last file, it has set the ontext to TF, but you can't rely on that, as that behavior is not guaranteed for the future (in fact, I'm going to change the context files from setting contexts, so that problem authors would be forced to set a context explicitly.

Finally, you should not load macro files that you don't actually use. For example, you have loaded contextCurrency.pl, which is not used in this problem. While it may make it easier for you to copy and paste from one problem to another, this causes WeBWorK to have to load and process files it doesn't need, and that happens each time the student views the problem or submits an answer. While it is not much for a single instance, if lots of students are submitting lots of answers, it can cause a significant impact on your server.

If you really feel you need to see the other macro files, at least comment them out in the problems that don't use them.

Davide