Open main menu

lensowiki β

Changes

Help:Array

8,983 bytes added, 23:48, 16 May 2006
m
h:hns cleanup
{{H:h}}[[Category:Editor handbook]]
'''Note:''' The techniques covered in this article depend on features unavailable before MediaWiki 1.6.
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.

==Function #switch==
The function [[ParserFunctions#switch|#switch]] provides the main array technique. Where this extension is available, the other methods are probably obsolete.

*<nowiki>{{#switch:3|1=Mon|2=Tue|3=Wed|4=Thu|5=Fri|6=Sat|7=Son|undefined}}</nowiki> gives {{#switch:3|1=Mon|2=Tue|3=Wed|4=Thu|5=Fri|6=Sat|7=Son|undefined}}
*<nowiki>{{#switch:5|1=Mon|2=Tue|3=Wed|4=Thu|5=Fri|6=Sat|7=Son|undefined}}</nowiki> gives {{#switch:5|1=Mon|2=Tue|3=Wed|4=Thu|5=Fri|6=Sat|7=Son|undefined}}
*<nowiki>{{#switch:0|1=Mon|2=Tue|3=Wed|4=Thu|5=Fri|6=Sat|7=Son|undefined}}</nowiki> gives {{#switch:0|1=Mon|2=Tue|3=Wed|4=Thu|5=Fri|6=Sat|7=Son|undefined}}

Using {{timc|Short DOW with switch}}:
*<nowiki>{{Short DOW with switch|3}}</nowiki> gives {{Short DOW with switch|3}}
*<nowiki>{{Short DOW with switch|5}}</nowiki> gives {{Short DOW with switch|5}}
*<nowiki>{{Short DOW with switch|0}}</nowiki> gives {{Short DOW with switch|0}}

Put "#default=" in front of the default value if it contains "=":

*<nowiki>{{#switch:advanced|simple=1+1=2|#default=999+999=1998}}</nowiki> gives {{#switch:advanced|simple=1+1=2|#default=999+999=1998}}

==Array technique using parameter defaults==
{{tim|Short DOW par def}}, based on [[Help:parameter default|parameter default]]s, 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 par def|ifu=|3=}}</nowiki> gives {{short DOW par def|ifu=|3=}}

<nowiki>{{short DOW par def|ifu=|6=|3=|5=|3=}}</nowiki> gives {{short DOW par def|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.

See also {{tim|m to ft par def}}.

==Array technique using CSS==
Unlike other methods, this method works only for final display, and only on modern browsers with enabled support for [[w:Cascading Style Sheets|CSS]].
The result cannot be used in expressions for template names, parameter names, parameter values, page names in links, etc.

Example:

{{tim|Short DOW css d}} contains (without the newlines):

<pre>
<span style="display:non{{{1|e}}}">Mon</span>
<span style="display:non{{{2|e}}}">Tue</span>
<span style="display:non{{{3|e}}}">Wed</span>
<span style="display:non{{{4|e}}}">Thu</span>
<span style="display:non{{{5|e}}}">Fri</span>
<span style="display:non{{{6|e}}}">Sat</span>
<span style="display:non{{{0|e}}}">Sun</span>
</pre>

;<nowiki><tt>{{short DOW css d|3=}}</tt></nowiki> gives <tt>{{short DOW css d|3=}}</tt>
;<nowiki><tt>{{short DOW css d|6=|3=|5=|3=}}</tt></nowiki> gives <tt>{{short DOW css d|6=|3=|5=|3=}}</tt>
;Without CSS support the result is <tt>MonTueWedThuFriSatSun</tt>

Again, 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.
A variant of this method uses the [[MediaWiki:common.css|common.css]] class [[w:WP:HIDE|hiddenStructure]], example:

{{tim|Short DOW css}} contains (without the newlines):

<pre>
<span class="hiddenStructur{{{1|e}}}">Mon</span>
<span class="hiddenStructur{{{2|e}}}">Tue</span>
<span class="hiddenStructur{{{3|e}}}">Wed</span>
<span class="hiddenStructur{{{4|e}}}">Thu</span>
<span class="hiddenStructur{{{5|e}}}">Fri</span>
<span class="hiddenStructur{{{6|e}}}">Sat</span>
<span class="hiddenStructur{{{0|e}}}">Sun</span>
</pre>

;<nowiki><tt>{{short DOW css|3=}}</tt></nowiki> gives <tt>{{short DOW css|3=}}</tt>
;<nowiki><tt>{{short DOW css|6=|3=|5=|3=}}</tt></nowiki> gives <tt>{{short DOW css|6=|3=|5=|3=}}</tt>
;Without CSS support the result is <tt>MonTueWedThuFriSatSun</tt>

==Comparison of the CSS method and the parameter default method==
Comparing the CSS method and the parameter default method, we see that each of the lines

<nowiki><span style="display:non{{{3|e}}}">Wed</span></nowiki>
<nowiki><span class="hiddenStructur{{{3|e}}}">Wed</span></nowiki>
<nowiki>{{{if{{{3|u}}}|Wed}}}</nowiki>

in a template T, called with

<nowiki>{{T|3=}}</nowiki>
<nowiki>{{T|3=}}</nowiki>
<nowiki>{{T|ifu=|3=}}</nowiki>

respectively, give Wed, and the empty string if 3 is left undefined.

Thus the parameter default method has similar functionality, but requires an extra parameter. It has the advantage that the result can be used in expressions for template names, parameter names, parameter values, page names in links, etc.

For comparison, suppose we would use the shorter word "ifu" instead of "HiddenStructure" (note that this would require that class "if" is undefined); then the lines are:

<nowiki><span style="display:non{{{3|e}}}">Wed</span></nowiki>
<nowiki><span class="if{{{3|u}}}">Wed</span></nowiki>
<nowiki>{{{ if{{{3|u}}}| Wed}}}</nowiki>

In the third method the wikitext in the template is 15 characters shorter than the second method, for each optional item, but each call is 5 characters longer.

In the second method conflicts with other class names have to be avoided, in the third method conflicts with other parameter names. The first method is best in this regard.

==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
}}

using {{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
|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>
----

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|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, and (for final display only) that using CSS, both 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.

==See also==
*[[w:en:Portal:Middle-earth/Featured article]] called in [[w:en:Portal:Middle-earth]] - array technique based on [[Help:parameter default|parameter default]]s
*{{tiw|en|Locale length}} - this template and some of its older versions apply several array techniques
*[[Help:Array (older methods)]]

{{h:f|enname=Array}}
Anonymous user