Help:Array

From The Scuba Wiki

Revision as of 20:37, 28 December 2009 by WikiSysop (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins +/-

This page explains various methods of creating a kind of associative array using templates.

Contents

Function #switch

The function #switch provides one of the two main array techniques.

  • {{#switch:3|1=Mon|2=Tue|3=Wed|4=Thu|5=Fri|6=Sat|7=Sun|undefined}} gives Wed
  • {{#switch:5|1=Mon|2=Tue|3=Wed|4=Thu|5=Fri|6=Sat|7=Sun|undefined}} gives Fri
  • {{#switch:0|1=Mon|2=Tue|3=Wed|4=Thu|5=Fri|6=Sat|7=Sun|undefined}} gives undefined

Using Template:Short DOW with switch containing "{{#switch:{{{1}}}|0=Sun|1=Mon|2=Tue|3=Wed|4=Thu|5=Fri|6=Sat|7=Sun|undefined}}<noinclude> [[Category:Date computing template|{{PAGENAME}}]]</noinclude>":

  • {{Short DOW with switch|3}} gives Wed
  • {{Short DOW with switch|5}} gives Fri
  • {{Short DOW with switch|0}} gives Sun

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

  • {{#switch:advanced|simple=1+1=2|#default=999+999=1998}} gives 999+999=1998

or, with the new preprocessor, use template:= ( talk edit history links ):

  • {{#switch:advanced|simple=1+1=2|999+999{{=}}1998}} gives 999+999Template:=1998

Producing multiple array elements in specified order

template:for ( talk edit history links ) allows not only to produce one array element, like above, but also more.

Examples:

{{for| |call=Short DOW|3}} gives "Template:1x"

{{for| |call=Short DOW|{{CURRENTDOW}}}} gives "Template:1x"

{{for| |call=Short DOW|7}} gives "Template:1x"

{{for| |call=Short DOW|3|{{CURRENTDOW}}|7}} gives "Template:1x Template:1x Template:1x"

{{for| |call=Short DOW|6|3|5|3}} gives "Template:1x Template:1x Template:1x Template:1x"

{{for|

|call=Short DOW|6|3|5|3}} gives:


Template:1x

Template:1x

Template:1x

Template:1x


Producing multiple array elements in standard order, without duplicates

In this method 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.

Using template:Short DOW ipv ( talk edit history links ):

{{short DOW ipv| |3}} gives "Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "[""

{{short DOW ipv| |6|3|5|3}} gives "Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["| }}Expression error: Unrecognised punctuation character "[""

{{short DOW ipv|

|6|3|5|3}} gives:


Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["|

}}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["|

}}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["|

}}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["|

}}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["|

}}Expression error: Unrecognised punctuation character "["{{ Expression error: Unrecognised punctuation character "["|

}}Expression error: Unrecognised punctuation character "["


The template uses template:bintodec ( talk edit history links ), which computes an integer of which the binary representation forms the Boolean array indicating which array elements are selected. This makes it easier to determine where separators are needed (after every selected item, provided that more items are following).

Arrays with a template for each element

An array with a template for each element seems primitive compared with using #switch, but an advantage was that this method was much less affected by the former pre-expand include size maximumhttp://en.wikipedia.org/wiki/Wikipedia:Template limits of 2,048,000 bytes.

A 1D array has elements which are templates with names of the form array-name index, a 2D array (matrix) has elements which are templates with names of the form array-name index1 separator index2. The indexes can be any text.

For example, on Meta there is for each Wikipedia language a template for its English name, e.g. template:eln de ( talk edit history links ). They are used on List of Wikipedias/sortable and can also be useful on similar lists. They form an alternative for template:n en ( talk edit history links ):

There is also an older, small collection of templates with names of the form "Lang_name_-_language referred to_(language used )" (in fact, the pattern is an example of a generalized form of the name pattern, with a text, here a closing parenthesis, after the second index) with codes used for the two languages; the contents of each template is the word for a language, expressed in the same or another language, e.g. Template:Lang name - de (fr) contains "Allemand", which is "German" in French. (See list; for diagonal elements such as Template:Lang name - de (de) there are, in addition, copies such as Template:Lang name - de). See also explanation page.

In the case of a 2D array separator and the possible values of index1 and index2 should be chosen such that there is no ambiguity; no separator is needed if at least one index is of fixed length, or e.g. if the first index consists of letters and the second of digits. The software does not need to parse index1 separator index2, but for convenient human parsing a separator such as a blank space may be preferred in some cases where it is not strictly needed.

Elements of a 1D array can e.g. be referred to inside a template with {{array-name {{{index}}}}} using parameter index, or similarly with a variable. Also a page can successively call each array element using {{array-name index}} with varying index.

In the case of a 2D array, a template may have a row index as parameter and produce a list based on that row of the matrix, or similarly for columns. See e.g. Template:List of Languages.

A disadvantage of having a template for each array element is that it is extremely cumbersome to copy a large array to another project, unless a bot is used; a sysop may also apply export and import, if those features are enabled.

An advantage is that the absence of a data value shows up as a link to a non-existing template, allowing an individual data value to be added easily. Depending on how fast the servers are, adding multiple values may be slower, sometimes much slower, than when fewer templates have to be edited.

Older methods

Array technique using parameter defaults

template:Short DOW par def ( talk edit history links ), based on parameter defaults, contains (without the newlines):

{{{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}}}|Sun}}}

{{short DOW par def|ifu=|3=}} gives Wed

{{short DOW par def|ifu=|6=|3=|5=|3=}} gives WedFriSat

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 template:m to ft par def ( talk edit history links ).

Array technique using CSS

Unlike other methods, this method works only for final display, and only on modern browsers with enabled support for CSS. The result cannot be used in expressions for template names, parameter names, parameter values, page names in links, etc.

Example:

template:Short DOW css d ( talk edit history links ) contains (without the newlines):

<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>
<tt>{{short DOW css d|3=}}</tt> gives MonTueWedThuFriSatSun
<tt>{{short DOW css d|6=|3=|5=|3=}}</tt> gives MonTueWedThuFriSatSun
Without CSS support the result is MonTueWedThuFriSatSun

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 common.css class hiddenStructure, example:

template:Short DOW css ( talk edit history links ) contains (without the newlines):

<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>
<tt>{{short DOW css|3=}}</tt> gives MonTueWedThuFriSatSun
<tt>{{short DOW css|6=|3=|5=|3=}}</tt> gives MonTueWedThuFriSatSun
Without CSS support the result is MonTueWedThuFriSatSun

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

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

in a template T, called with

{{T|3=}}
{{T|3=}}
{{T|ifu=|3=}}

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:

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

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:

{{array
 |index=index
 |1=Mon
 |2=Tue
 |3=Wed
 |4=Thu
 |5=Fri
 |6=Sat
 |0=Sun
}}

using template:array ( talk edit history links ), containing {{{{{{index}}}|{{{default|}}}}}}.

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): template:short DOW ( talk edit history links ), containing:


{{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.
}}

Examples:

{{Short DOW|3}} gives Wed

{{Short DOW|{{CURRENTDOW}}}} gives Thu

{{Short DOW|7}} gives 7 is not a valid weekday number.


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:

{{t Short DOW|3}} using template:t Short DOW ( talk edit history links ), containing:


{{array
  |{{{1}}}
  |1=Mon
  |2=Tue
  |3=Wed
  |4=Thu
  |5=Fri
  |6=Sat
  |0=Sun
  |default={{{1}}} is not a valid weekday number.
}}

(hence using implicit name "1" instead of "index") gives "3 is not a valid weekday number.". The implicit 1=3 is overwritten by "1=Mon", and {{{Mon}}} 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


+/-

Links to other help pages

Help contents
Meta | Wikinews | Wikipedia | Wikiquote | Wiktionary | commons: | mw: | b: | s: | mw:Manual | google
Versions of this help page (for other languages see below)
Meta | Wikinews | Wikipediahttp://en.wikipedia.org/Help:Array | Wikiquote | Wiktionary
What links here on Meta or from Meta | Wikipedia | MediaWiki
Reading
Go | Search | Stop words | URL | Namespace | Page name | Section
Backlinks | Link | Piped link | Interwiki link | Redirect | Category | Image page
Logging in and preferences
Logging in | Preferences | User style
Editing
Advanced editing | Editing FAQ | Edit toolbar | Export | Import | Shortcuts
Tracking changes
Recent changes (enhanced) | Related changes | Watching pages | Diff
Page history | Edit summary | User contributions | Minor edit | Patrolled edit
Style & formatting
Wikitext examples | Reference card | HTML in wikitext | List | Table | Sorting | Colors
Special input and output
Inputbox | Special characters | Displaying a formula | Images (uploads) | EasyTimeline
Advanced functioning
Template | Advanced templates | Parser function | ParserFunctions | Parameter default
Variable | Magic word | System message | Substitution | Array | Calculation
Page management
Starting a new page | Renaming (moving) a page | Protecting pages | Deleting a page
Special pages
Talk page | Testing | Sandbox | CentralNotice

Template:-

Personal tools
support the site