WeBWorK Main Forum

Dynamic tables

Dynamic tables

by Sean Fitzpatrick -
Number of replies: 3

Is there any way to do dynamic tables in WeBWorK (i.e. of variable size)?

I have a colleague who is trying to find a way to do boolean logic problems for a CS course, and she needs tables whose size is not hard-coded in the problem. This could be for both answer blanks and solutions.

In reply to Sean Fitzpatrick

Re: Dynamic tables

by Glenn Rice -
You can construct an array with the entries of the table, and then pass them to the DataTable (or LayoutTable) method of niceTables.pl. If there are answer rules in the table, then you will need to use ANS calls to associate answer evaluators with the answer rules. See https://openwebwork.github.io/pg-docs/sample-problems/problem-techniques/DataTables.html. You might also want to see https://github.com/drgrice1/pg/blob/sample-problems-improvements/tutorial/sample-problems/ProblemTechniques/DataTables.pg. That is from a pull request that has not been merged yet in which I updated the sample problem to use the new PGML table syntax.
In reply to Glenn Rice

Re: Dynamic tables

by Sean Fitzpatrick -

Thanks Glenn. I think what she is looking for is a way to have a single question, where different versions of that question might have varying numbers of variables and operators, so that the number of rows and columns depends on random variables.

But it sounds like what you might be saying is that for a given seed, you can put all the answer evaluators into an array, and then pass that array to data tables? Possibly that could work.

In reply to Sean Fitzpatrick

Re: Dynamic tables

by Glenn Rice -

Yes, the number of rows and columns of the table can be variable depending on the random parameters.  Arrays can be dynamically created in numerous ways.  You can push or unshift elements in loops, or just assign to a particular index (even an index that doesn't already exist in the array).  You can even have a variable number of answer rules.  You will probably need additional arrays to contain answer rules in that case.