Programmer Guide/Command Reference/COND: Difference between revisions
From STX Wiki
Jump to navigationJump to search
m (Text replace - "S_TOOLS-STx" to "{{STX}}") |
m (Text replace - "Command_Reference_Intro" to "Concepts") |
||
Line 3: | Line 3: | ||
Generally, the conditional assignment takes the following form: | Generally, the conditional assignment takes the following form: | ||
target := COND [[Programmer_Guide/ | target := COND [[Programmer_Guide/Concepts/Conditional_Expressions|condition]] ? expr1 : expr2 | ||
;<code>target</code>: is a normal assignment target, usually the name of an {{STX}} variable, e.g. <code>#var</code>. | ;<code>target</code>: is a normal assignment target, usually the name of an {{STX}} variable, e.g. <code>#var</code>. | ||
;<code>condition</code>: is a [[Programmer_Guide/ | ;<code>condition</code>: is a [[Programmer_Guide/Concepts/Conditional_Expressions|conditional expression]] like used with the <code>[[User Guide/Workspace/Parameter Processing|IF]]</code> statement and the miscellaneous loop commands, e.g. the string <code>$#a == 7 || $#a == 42</code> (for more examples, see the <code>[[User Guide/Workspace/Parameter Processing|IF]]</code> statement). | ||
:Note that with conditional {{STX}} expressions there must always be intervening whitespaces between operators and their arguments (unless the argument is quoted). So, both <code>$#a == 7</code> and <code>'$#a'=='7'</code> are valid expression, whereas <code>$#a==7</code> is not. | :Note that with conditional {{STX}} expressions there must always be intervening whitespaces between operators and their arguments (unless the argument is quoted). So, both <code>$#a == 7</code> and <code>'$#a'=='7'</code> are valid expression, whereas <code>$#a==7</code> is not. | ||
;<code>expr1</code> and <code>expr2</code>: Both <code>expr1</code> and <code>expr2</code> may be any expressions that may be normally used in an {{STX}} <code>:=</code> assignment, with the only exception of a <code>COND</code> expression (meaning that conditional assignments must not be nested for the time being). If, at runtime, the conditional expression <code>condition</code> evaluates to truth, the value determined by the first expression, <code>expr1</code>, will be assigned to <code>target</code>. If <code>condition</code> evaluates to falsehood, it will be the second expression, <code>expr2</code>, that gets assigned to target. | ;<code>expr1</code> and <code>expr2</code>: Both <code>expr1</code> and <code>expr2</code> may be any expressions that may be normally used in an {{STX}} <code>:=</code> assignment, with the only exception of a <code>COND</code> expression (meaning that conditional assignments must not be nested for the time being). If, at runtime, the conditional expression <code>condition</code> evaluates to truth, the value determined by the first expression, <code>expr1</code>, will be assigned to <code>target</code>. If <code>condition</code> evaluates to falsehood, it will be the second expression, <code>expr2</code>, that gets assigned to target. | ||
You may use any {{STX}} [[Programmer_Guide/ | You may use any {{STX}} [[Programmer_Guide/Concepts/Conditional_Expressions|conditional expression]] you like for <code>condition</code> with the exception of conditions themselves containing the <code>COND</code> keyword. Equally, there is no restriction on <code>expr1</code> and <code>expr2</code>, except that they must not be built up from <code>COND</code> expressions themselves, either. It is even (and also) possible to use command substitutions (<code>$(...)</code>), nesting them as deeply as one feels inclined to. | ||
=== Notes === | === Notes === |
Revision as of 14:45, 13 April 2011
The COND
command performs a conditional assignment.
Generally, the conditional assignment takes the following form:
target := COND condition ? expr1 : expr2
target
- is a normal assignment target, usually the name of an STx variable, e.g.
#var
. condition
- is a conditional expression like used with the
IF
statement and the miscellaneous loop commands, e.g. the string$#a == 7 || $#a == 42
(for more examples, see theIF
statement). - Note that with conditional STx expressions there must always be intervening whitespaces between operators and their arguments (unless the argument is quoted). So, both
$#a == 7
and'$#a'=='7'
are valid expression, whereas$#a==7
is not. expr1
andexpr2
- Both
expr1
andexpr2
may be any expressions that may be normally used in an STx:=
assignment, with the only exception of aCOND
expression (meaning that conditional assignments must not be nested for the time being). If, at runtime, the conditional expressioncondition
evaluates to truth, the value determined by the first expression,expr1
, will be assigned totarget
. Ifcondition
evaluates to falsehood, it will be the second expression,expr2
, that gets assigned to target.
You may use any STx conditional expression you like for condition
with the exception of conditions themselves containing the COND
keyword. Equally, there is no restriction on expr1
and expr2
, except that they must not be built up from COND
expressions themselves, either. It is even (and also) possible to use command substitutions ($(...)
), nesting them as deeply as one feels inclined to.
Notes
- The
COND
command is processed by the loader, and is therefore not available in the command line interface. - The
COND
command may not contain a nestedCOND
command.
Examples
#min := cond $#a < $#b ? $#a : $#b // calculate minimum of #a and #b #abs := cond $#a < 0 ? num -$#a : $#a // calculate absolute value of #a #abs := cond $#a < 0 ? eval $#a*(-1) : $#a // an alternative to the above #absdiff := cond $#a > $#b ? eval $#a-$#b : eval $#b-$#a // absolute difference #len := cond $(length $#a) > 0 ? length $#a : length $#b // length of #a or, if empty, of #b
See the file conditional_assignment.sts
for further working examples.