Variable substitution - PGML

From WeBWorK_wiki
Revision as of 16:43, 10 May 2015 by Dpvc (talk | contribs) (Add hash and array element examples)
Jump to navigation Jump to search

Variable Substitution

Frequently, you will have computed or randomly generated values that you want to include in the text of your problem. To insert the value of a $a into PGML, use [$a]. Such values can be inserted into the text anywhere, including inside math delimiters.

 Suppose a manufacturer produces [$n] cars at a cost of [$cost] per car.


 Suppose [`f(x) = [$f]`].  Then [`f'(x) = `] [_________________].


 Suppose [: f(x) = [$f] :].  Then [: f'(x) = :] [_________________].

In the first example, the values of $a and $cost are inserted as plain text. In the second, example, if $f is a MathObject Formula, then its TeX form will be inserted and processed as TeX code (due to the [`...`] delimiters. In the third example, the Formula object's algebra string will be inserted into the math and processed in the Typeset Context to generate the mathematical output.

You can access array elements and hash entries as well.

 @A = (1,2,3); %B = (x => 1, y => 2); $C = [1,2,3]; $D = {x => 1, y => 2};
 Array element: [$A[1]] produces 2  
 Hash element: [$B{x}] produces 1
 Array de-reference: [$C->[0]] produces 1
 Hash de-reference: [$D->{y}] produces 2