Help:Array: Difference between revisions

Content deleted Content added
TRANSWIKI: Copied from Meta:Help:Array.
TRANSWIKI: Copied from Meta:Help:Array.
Line 2:
This pages explains various methods of creating a kind of [[w:en:associative array|associative array]] using [[Help:Template|templates]].
 
Note that producing an array element for a given index value corresponds to a case statement or switch.
==Main array technique==
This method of creating a data array using [[Help:Template|templates]] seems to be the most suitable and is therefore recommended.
 
==Array technique using parameter defaults==
An example of an array is {{tim|short DOW}}, containing:
{{tim|Short DOW alt 2}} contains (without the newlines):
 
<pre>
{{{if{{{1|u}}}|Mon}}}
{{{if{{{2|u}}}|Tue}}}
{{{if{{{3|u}}}|Wed}}}
{{{if{{{4|u}}}|Thu}}}
{{{if{{{5|u}}}|Fri}}}
{{{if{{{6|u}}}|Sat}}}
{{{if{{{0|u}}}|Son}}}
</pre>
 
<nowiki>{{short DOW alt 2|ifu=|3=}}</nowiki> gives {{short DOW alt 2|ifu=|3=}}
 
<nowiki>{{short DOW alt 2|ifu=|6=|3=|5=|3=}}</nowiki> gives {{short DOW alt 2|ifu=|6=|3=|5=|3=}}
 
The results are in the index order according to the template content, not in the order of the parameters in the template call. Multiple occurrences are removed.
 
==Array technique using a small auxiliary template==
Example:
 
<nowiki>{{array</nowiki>
|index=''index''
|1=Mon
|2=Tue
|3=Wed
|4=Thu
|5=Fri
|6=Sat
|0=Sun
}}
 
It usesusing {{tim|array}}, containing <code><nowiki>{{{{{{index}}}|{{{default|}}}}}}</nowiki></code>.
 
In the case of a constant ''index'' this can be useful for selection from predetermined options by a small edit. Alternatively ''index'' can be an expression depending on a variable, or, if the text is in a template, on a parameter.
 
Note that if only one template is used, the difference with the method above is that the array data are outside the template: they are provided in the template call.
 
Example where the text is in another template (with also a default value): {{tim|short DOW}}, containing:
----
<pre>{{array
Line 19 ⟶ 57:
}}</pre>
----
 
It uses {{tim|array}}, containing <code><nowiki>{{{{{{index}}}|{{{default|}}}}}}</nowiki></code>.
 
Examples:
Line 62 ⟶ 98:
 
<code><nowiki>{{foreach|call=Short DOW|sep=-|3|{{CURRENTDOW}}|7}}</nowiki></code> gives "{{foreach|call=Short DOW|sep=-|3|{{CURRENTDOW}}|7}}"
 
<code><nowiki>{{foreach|call=Short DOW|6|3|5|3}}</nowiki></code> gives "{{foreach|call=Short DOW|6|3|5|3}}"
 
Compare the array technique using parameter defaults, above.
 
Differences:
*Above only one template is used instead of three.
*Above the results are in standard index order, here in the order of the parameters in the template call.
*Above multiple occurrences are removed.
 
==Method of erasing specification for erasure==
Line 317 ⟶ 362:
==See also==
*[[w:en:Wikipedia talk:Avoid using meta-templates#Logic templates|Logic templates]] (discussion)
*[[w:en:Portal:Middle-earth/Featured article]] called in [[w:en:Portal:Middle-earth]] - array technique based on [[Help:parameter default|parameter default]]s
 
{{h:f|enname=Array}}