Technical References - [switch]

Executes the WebDNA inside the only [case] context which matches the given value.

[switch Value]Series of [case]...[/case] contexts[/switch]

To display some HTML (or execute some WebDNA) from a list of known text options, put the text value inside a [switch] context. For each possible option, put a [case] context inside the [switch]. You may optionally specify a default case by inserting a [default] context. The [default] context must be the very last context inside the [switch].

[text]x=5[/text]
[switch value=[x]]
[case value=1]
The value of x was 1
[/case]
[case value=2]
The value of x was 2
[/case]
[default]
The value of x was neither 1 nor 2; it was [x]
[/default]
[/switch]

[text]title=Mrs[/text]
[switch value=[title]]
[case value=Mr]
You're a male
[/case]
[case value=Mrs]
You're a female
[/case]
[/switch]

In the first example above, the text "The value of x was neither 1 nor 2; it was 5" will display, because the two cases for "1" and "2" did not match the actual value of x, which was "5." In the second example above, the text "You're a female" will display. Any WebDNA inside the other [case] contexts will not execut, and any text inside those contexts will not display.

The values are compared as case-insensitive text only. This means the number "1.0" is not the same as the number "1" when determining which of the [case] contexts to execute.



From WebDNA version 8.1, [switch] accepts the whole set of comparisons:

ComparisonExample
equal =[If "[username]" = "SAGEHEN"] variable [username] is equal to SAGEHEN
not equal ![If [random] ! 45] random number is not 45
contains ^[If "[browsername]" ^ "Mozilla"] variable [browsername] contains the text Mozilla
begins with ~[If "[ipaddress]" ~ "245.078.013"] variable [ipaddress] begins with 245.078.013

Notice the IP address has been typed with 3 digits in each portion ofthe address. this is very important for making these comparison workas expected.
less than <[If [random] < 50] random number is less than 50
greater than >[If [lastrandom] > 25] last random number is greater than 25
divisible by\[If [index] \ 3] variable [index] is divisble by 3
or|[If (5>4) | (1<3)] Boolean comparison: if either side of the operatoris true, then the comparison is true
and&[If (5>4) & (1<3)] Boolean comparison: if both sides of theoperator are true, then the comparison is true
Delimiter Example
Quoted Text
"..."[If "Hello" ^ "hell"] All text must be surrounded by quotes
Numbers [If 12.5 < 13.2] Numbers do not need to be delimited; they function the same as in a [math] context
Dates{}[If {[date]} > {9/7/1963}] Dates must be enclosed in curly braces to distinguish them from regular numbers
Times{}[If {[time]} > {12:31:00PM} Times must be enclosed in curly braces to distinguish them from regular numbers
Parentheses(...)[If (3>1) & ("a"<"b")] You may collect groupsof items in parentheses in order to force the order of evaluation


To maintain compatibility with previous versions, if the [case] statement sets the parameter "value" as in this example:
[case value=13]
Then it assumes they are the old-style switch statements.

However, if it doesn't see "value=", it assumes there is an equation to evaluate, just like with [if]. You can mix/match both types in the same switch block.


[text]x=3[/text]
[switch value=[x]]
[case [x]=1]
The value of x was 1
[/case]
[case [x]>2]
The value of x was > 2
[/case]
[case value=3]
The value of x was 3
[/case]
[default]
The value of x was neither 1 nor 3 nor greater than 2; it was [x]
[/default]
[/switch]


This code outputs:
The value of x was > 2
The value of x was 3

If you set value= in the [switch] statement, then that is used only for the [case] statements where you also set value=. The value= you set in the [switch] statement is completely ignored for any [case] statements where you don't set value=.

So as an example, this does NOT work at all:
[switch value=[x]]
[case value>23]
Because you are literally comparing the text "value" to the number 23.

Also, this does NOT work either:
[switch value=[x]]
[case [value]>23]
value is a parameter, it is not defined as a variable. So [value] doesn't evaluate as anything in the equation.

However, this DOES work:
[text]value$[/text]
[switch value=[x]]
[case [value]>23]
This works because value was defined as a variable outside of the switch.

This works too:
[text]value$[/text]
[switch]
[case [value]>23]
If you don't use value= statements in the [case] statements, you don't have to set it in the [switch] statement.


[text]x=3[/text]
[text]y=1[/text]


[switch value=[x]] >>>>>>> value=[x] to maintains compatibility
[case [x]=1]
The value of x was 1
[/case]
[case [y]>2]
The value of y was > 2
[/case]
[case value=4] >>>>>>> this is to maintain compatibility
The value of x was 4
[/case]
[default]
The value of x was not 1 or 4 and the value of y was not greater than 2
[/default]
[/switch]


works perfectly fine. The old compatible style statements still work and the new ones do too. For example, x=4 and y=3 results in:
The value of y was > 2 The value of x was 4

DOWNLOAD WEBDNA NOW!

Top Articles:

[biotype]

BioType is a behavioral biometrics WebDNA function based on ADGS research and development (from version 8...

WebDNA Modules

A list of the currently available modules...

WebDNA Libraries

A list of available libraries for WebDNA...

F.A.Q

A compilation of some user's questions...

Tips and Tricks

A list of user-submitted tips ...

AWS Raw WebDNA LAMP-Plus WebServer

Amazon Web Services (AWS) README for Machine Image ID...

Related Readings:

[copyfolder]

Copy a folder and all its contenton your webspace...

[showif]

[showif Comparison]Show This HTML[/showif]...

[flushcache]

...

[hideif]

[HideIf Comparison]Hide This HTML[/HideIf]...

[SQL]

Performs a SQL statement on an ODBC data source...

[listchars]

Breaks a string of text into separate characters...