Help:Array: Difference between revisions

Content deleted Content added
TRANSWIKI: Copied from Meta:Help:Array.
TRANSWIKI: Copied from Meta:Help:Array.
Line 1:
{{H:h|editor toc}}
 
An example of an array is {{tim|short DOW}}, containing:
A method to create a data array using [[Help:Template|templates]] is as follows. A template, the so-called '''concatenation template''', produces the values of all its parameters. It is called by a '''data template''' that assigns all data to the parameters, then erases the values of the parameters of which the names are its own parameters, resulting in erasing all values except the desired one. The '''root template''' provides the selection of values to be erased: all parameters are specified for erasure from the list to be displayed, but for the desired one the specification for erasure is itself erased.
----
<pre>{{array
|index={{{1}}}
|1=Mon
|2=Tue
|3=Wed
|4=Thu
|5=Fri
|6=Sat
|0=Sun
|default={{{1}}} is not a valid weekday number.
}}</pre>
----
 
It uses {{tim|array}}, containing <code><nowiki>{{{{{{index}}}|{{{default|}}}}}}</nowiki></code>.
 
Examples:
 
<code><nowiki>{{Short DOW|3}}</nowiki></code> gives <code>{{Short DOW|3}}</code>
 
<code><nowiki>{{Short DOW|{{CURRENTDOW}}}}</nowiki></code> gives <code>{{Short DOW|{{CURRENTDOW}}}}</code>
 
<code><nowiki>{{Short DOW|7}}</nowiki></code> gives <code>{{Short DOW|7}}</code>
 
 
Another version of the two templates could use a term other than "index", e.g. the empty string. However, it should be a term that itself is not used as array index:
 
<code><nowiki>{{t Short DOW|3}}</nowiki></code> using {{tim|t Short DOW}}, containing:
----
<pre>{{array
|{{{1}}}
|1=Mon
|2=Tue
|3=Wed
|4=Thu
|5=Fri
|6=Sat
|0=Sun
|default={{{1}}} is not a valid weekday number.
}}</pre>
---- (hence using implicit name "1" instead of "index") gives "<code>{{T Short DOW|3}}</code>". The implicit 1=3 is overwritten by "1=Mon", and <code>{{{Mon}}}</code> would be produced; since it is undefined, the default is produced, with in this case a confusing error message: not the input is incorrect, but the template itself.
 
See also {{tiw|en|switch}}, where parameter names consist of "case: ", followed by the index.
 
==Foreach==
{{tim|foreach}} allows not only to produce one array element, like above, but also more.
 
Examples:
 
<code><nowiki>{{foreach|call=Short DOW|3}}</nowiki></code> gives "{{foreach|call=Short DOW|3}}"
 
<code><nowiki>{{foreach|call=Short DOW|{{CURRENTDOW}}}}</nowiki></code> gives "{{foreach|call=Short DOW|{{CURRENTDOW}}}}"
 
<code><nowiki>{{foreach|call=Short DOW|7}}</nowiki></code> gives "{{foreach|call=Short DOW|7}}"
 
<code><nowiki>{{foreach|call=Short DOW|3|{{CURRENTDOW}}|7}}</nowiki></code> gives "{{foreach|call=Short DOW|separator=-|3|{{CURRENTDOW}}|7}}"
 
==Other method==
AAnother method to create a data array using [[Help:Template|templates]] is as follows. A template, the so-called '''concatenation template''', produces the values of all its parameters. It is called by a '''data template''' that assigns all data to the parameters, then erases the values of the parameters of which the names are its own parameters, resulting in erasing all values except the desired one. The '''root template''' provides the selection of values to be erased: all parameters are specified for erasure from the list to be displayed, but for the desired one the specification for erasure is itself erased.
 
==Language name example==
Line 51 ⟶ 110:
 
==Plain numbers as index values==
<nowiki>{{</nowiki>t array|square|3}} using {{timcnl|t array}} and {{timcnl|square}} and {{tim|concat1440}} gives {{t array|square|3}}.
 
Nested:
 
<nowiki>{{t array|square|{{array|square|2}}}}</nowiki> gives {{t array|square|{{t array|square|2}}}}.
 
For larger arrays, extend the three lists: ''k''=''k'', ''k''=''data'', {{{''k'' }}}=. The fourth list is already available for 1440 index values.
Line 61 ⟶ 120:
===Other example===
 
<nowiki>{{</nowiki>t array|array demo|3}} using {{timcnl|t array}} and {{timcnl|array demo}} and {{tim|concat1440}} gives {{array|array demo|3}}.
 
==Comparison with other method==