|
Synopsis
During the process and rendering of a subtopic, it is possible to perform mathematical operations on variables. This is most useful when iterating through an array or performing some other repetitive function.
Available Operations
<#math: $variable += $x#> ## Adds $x to $variable
<#math: $variable -= $x#> ## Subtracts $x from $variable
<#math: $variable *= $x#> ## Multiplies $x by $variable
<#math: $variable /= $x#> ## Divides $variable by $x
<#math: $variable %= $x#> ## Takes $variable mod $x
To perform a mathematical operation, the variable you are operating upon (in the above example, $variable) must be an entirely alphanumeric variable, or it can be a variable of the form $hash->{key}. The argument (in the above example, $x) can be a constant or any kind of variable recognized by the Discus Template Language. When a mathematical operation takes place, the result is placed back into the variable you are operating upon.
If you attempt to divide by zero, or take the modulus by zero, nothing happens (your command is ignored).
Mathematical statements such as these must be placed on a line of their own (they cannot be used within an in-line IF-THEN-ELSE block). You are limited to one operation per line. At times, it may be more efficient to use the mathdefine command, especially if many successive mathematical operations must be carried out.
An Example
<#define: $i = 3#> ## $i = 3 (duh)
<#math: $i += 4#> ## $i is now 3+4=7
<#math: $i -= 1#> ## $i is now 7-1=6
<#math: $i *= 2#> ## $i is now 6*2=12
<#math: $i /= 3#> ## $i is now 12/3=4
<#math: $i %= 0#> ## $i is still 4; this illegal math operation ignored
$i ## Prints 4
|