switch()
switch()
Section titled “switch()”switch(<str>, <expr1>, <list1>[, ... , <exprN>, <listN>][, <default>])
switchall(<str>, <expr1>, <list1>[, ... , <exprN>, <listN>][, <default>])
case(<str>, <expr1>, <list1>[, ... , <exprN>, <listN>][, <default>])
caseall(<str>, <expr1>, <list1>[, ... , <exprN>, <listN>][, <default>])
These functions match <string> against the <expr>essions, returning the corresponding <list>. If nothing is matched, the <default> is returned. switch() and case() return the <str> for the first matching <expr>, while switchall() and caseall() return the corresponding <list> for all <expr>s which match.
switch() and switchall() use wildcard and lt/gt <expr>s, as described in SWITCH WILDCARDS. case() and caseall() do a case-sensitive exact match, like member() or comp(). In this case, $0-$9 will be set to the text that the nth wildcard character met.
If the string ”#$” appears in the <list> to be evaluated, it will be replaced with the evaluated value of <str> /before/ evaluation of <list>. This is not done in case() and caseall(), for TinyMUSH 3 compatibility. Note that this replacement happens before evaluation, which makes it unsafe when <str> contains user input, and makes it unsuitable for use in nested switch()es. It is strongly recommended you use the %$\<n\> substitution or stext() function instead, which solves these problems.
::: seealso
Pattern selection examples
Section titled “Pattern selection examples”Examples:
say switch(test, *a*, foo, *b*, bar, *t*, neat, baz)You say, "neat"> say switchall(ack, *a*, foo, *b*, bar, *c*, neat, baz)You say, "fooneat"> say switch(moof, *a*, foo, *b*, bar, *t*, neat, baz)You say, "baz"> say switch(moof, *a*, foo, *b*, bar, *t*, neat, #$)You say, "moof"> say case(moof, *f, foo, moof, bar, baz)You say, "bar"> say switch(foo bazaar,f?o b*r,$0-$1)You say, "o-azaa"