Documentation

#SWITCH

SWITCH

Syntax: #SW (expression) {command} (expression) {command} ... {else_command}

Executes a command based upon which expression is true. This is similar to a giant set of if-then-elseif-elseif...else commands. Each expression is followed by a set of commands. Each expression is tested until one is found that is true. When one is found that is true, then the commands listed just after this expression are executed. If no expressions are true and there is an extra set of commands at the end, then this final "else" command is executed.

SWITCH example

a=10
#SWITCH (@a=1) {a is one} (@a < 0) {a is negative} (@a=10) {a is ten} (@a >= 10) {a is ge ten} {nothing matched}
displays "a is ten". Notice that the @a>=10 command doesn't execute because the @a=10 expression already matched. Once an expression matches, the rest of the command is ignored.

Note that you can implement an "else" clause at the end by using an expression of "1" or true. This will cause the final value to be returned if no other expressions matched.

To use the multiline syntax in TeSSH, the first expression and command must be on the same line as the #SWITCH, and each new expression and command must be on their own lines, indented from the first. For example:

#SWITCH (@a=1) {a is one}
(@a < 0) {a is negative}
(@a=10) {a is ten}
(@a >= 10) {a is ge ten}
{nothing matched}

A shortcut exists for switch commands that check a range of values of a single variable:

#switch (@a)
(0) {a is 0}
("blueberries") {a is "blueberries"}
("") {a is an empty string}
{nothing matched}

is equivalent to:

#switch (@a=0) {a is 0}
(@a="blueberries") {a is "blueberries"}
(@a="") {a is an empty string}
{nothing matched}

Add comment

Login or register to post comments