Difference between revisions of "Template:Sw/doc"
WikiMaster (Talk  contribs) (Create template.) 
(No difference)

Latest revision as of 17:34, 15 March 2012
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:
 "{{swp d p q }}" gives " q ".
 "{{swr d p q }}" gives " d ".
 "{{swp d p q r s }}" gives " q ".
 "{{swr d p q r s }}" gives " s ".
 "{{swt d p q r s }}" gives " d ".
 "{{swp d p q r s t u }}" gives " q ".
 "{{swr d p q r s t u }}" gives " s ".
 "{{swt d p q r s t u }}" gives " u ".
 "{{swv d p q r s t u }}" gives " d ".
 "{{swv d p q r s t u v w }}" gives " w ".
 "{{swv d p q r s t u v w x y }}" gives " w ".
 "{{swx 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