Template:Sw/doc
This template is like the #switch function, but preserves newlines and spaces from the start and end of the possible values (not from the values used in the conditions, if that is desired, put them all in quotes). A default value is required as second parameter:
- "{{sw|p| d |p| q }}" gives " q ".
- "{{sw|r| d |p| q }}" gives " d ".
- "{{sw|p| d |p| q |r| s }}" gives " q ".
- "{{sw|r| d |p| q |r| s }}" gives " s ".
- "{{sw|t| d |p| q |r| s }}" gives " d ".
- "{{sw|p| d |p| q |r| s |t| u }}" gives " q ".
- "{{sw|r| d |p| q |r| s |t| u }}" gives " s ".
- "{{sw|t| d |p| q |r| s |t| u }}" gives " u ".
- "{{sw|v| d |p| q |r| s |t| u }}" gives " d ".
- "{{sw|v| d |p| q |r| s |t| u |v| w }}" gives " w ".
- "{{sw|v| d |p| q |r| s |t| u |v| w |x| y }}" gives " w ".
- "{{sw|x| d |p| q |r| s |t| u |v| w |x| y }}" gives " y".
The template can be easily extended to allow more parameters.
#switch
A call of #switch
essentially takes an n × 2 array (lookup table), with key values in the first column and result values in the second column, a default value, and a control value, and returns the result value corresponding to the first key value which is equal to the control value, if any, and otherwise the default value.
The call of #switch
corresponds to a nesting of #ifeq
, so equality is defined in the same way as for #ifeq
:
{{sw| 1e3 | 0 | 1000 | 1 }}
gives " 1 ".{{#switch: 1e3 | 1000 = 1 | 0 }}
gives "1".{{sw| e | 0 | E | 1 }}
gives " 0 ".{{#switch: e | E = 1 | 0 }}
gives "0".{{sw| pi | 0 | PI | 1 }}
gives " 0 ".{{#switch: pi | PI = 1 | 0 }}
gives "0".
A switch can be used to store data. This is done in w:wp:WikiProject Tabular Data. However, retrieval gives a very high preprocessor node count of twice the number of cases until the match, so with a maximum of twice the total number of cases, which is twice the already high count for a template call with named parameters, see Help:Array#Comparison of named parameters and switch.
See also