Help:Array (older methods)

From Qi-Hardware
Jump to: navigation, search

Template:H:h This page is an extension of Help:Array and explains various older methods of creating a kind of associative array using templates.

Note that producing an array element for a given index value corresponds to a case statement or switch.


[edit] Method of erasing specification for erasure

This method of creating a data array using templates is not recommended.

[edit] Description

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.

[edit] Language name example

{{langname|de}} gives Template:Langname:

The "root template"


calls the "data template"


which calls the "concatenation template"


[edit] A 24 by 60 array

A concatenation template for general use, with unnamed parameters 1 - 1440, is Template:Tim.

The root template Template:Tim is for a 2D array of size 24 × 60, with index values "00:00" - "23:59". Thus it can also take a time as index value, in particular the result of {{CURRENTTIME}}.

A second set of index values is used in defining the array elements. These index values may or may not be the same as the first set. In the example above they are the same, here they are not, we have the index values 1 - 1440. Thus the template contains 1440 pieces of code of the form "00:04=5". The template is called with two parameters: the array name (i.e. the name of the data template) and the index pair, e.g. "00:04", which can also be seen as a single index.

A small modification fixes the first (part of the) index to 0: Template:Tim.

A matching example data template is Template:Tim. The first part contains 1440 pieces of code of the form "5=abc". The second part contains 1440 pieces of code of the form "=". This part can be reused for other data arrays with these index values.

Template:Tim, containing:

{{concat1440|2=Amsterdam|3=Rotterdam|4=The Hague|12=Amersfoort|17=01|{{{00:01}}}=|{{{00:02}}}=|{{{00:03}}}=|{{{00:04}}}=|{{{00:05}}}=|{{{00:06}}}=|{{{00:07}}}=|{{{00:08}}}=|{{{00:09}}}=

shows that the first part can contain less than 1440 data, while for the second part this is also the case, and this part may contain more than the first part, for convenient expanding of the array by just adding to the first part.


{{array1440|city array demo|00:02}} gives Template:Array1440

{{array1440|city array demo|00:03}} gives Template:Array1440

{{array60|city array demo|02}} gives Template:Array60

Example of using array elements as parameters:

{{t2|{{array60|city array demo|01}}|{{array60|city array demo|02}}}} using Template:Timc gives Template:T2

Examples of using a template result as (or in) an array index:

{{array60|city array demo|{{x2|1}}}} gives Template:Array60.

{{array60|city array demo|{{array60|city array demo|16}}}} gives Template:Array60.

[edit] Plain numbers as index values

{{t array|square|3}} using Template:Timcnl and Template:Timcnl and Template:Tim gives Template:T array.


{{t array|square|{{array|square|2}}}} gives Template:T array.

For larger arrays, extend the three lists: k=k, k=data, {{{k }}}=. The fourth list is already available for 1440 index values.

[edit] Other example

{{t array|array demo|3}} using Template:Timcnl and Template:Timcnl and Template:Tim gives Template:Array.

[edit] An array method using an auxiliary template for each value of the index used

An array method using an auxiliary template for each value of the index used (which can be reused in other arrays) is as follows (an alternative for "P" is possible, but consistency is desirable):

  • For all values of the index used, Template:Pvalue-of-index is defined, with the content {{{value-of-index }}} (see list for this project and list for the English Wikipedia). Where suitable, indexes of various arrays should be named the same, so that these templates can be reused.
  • An array has the form of a template with the contents
    • {{p{{{1}}}|value-1-of-index =value-of-array-element-1 |value-2-of-index =value-of-array-element-2 |..}}
  • or if index values 1,2,3,.. are used, simply
    • {{p{{{1}}}|value-of-array-element-1 |value-of-array-element-2 |..}}
  • An array element is referred to as {{template name |index }}.

Alternatively the parameter {{{1}}} is replaced by a variable, as in Template:Nsn (name space number). Since for every applicable value of the variable a template has to be created, this is mainly suitable for variables that do not take too many values, and also in the case that the template is only applied in cases where a suitable subset of values applies.

In the case of a parameter {{{1}}} there is no reason to choose the parameter names (before the "=", and in the tag in the corresponding template) differently from the corresponding index values (the template names without the "P"); even the empty string is a valid parameter name.

One has to decide whether a blank space or an underscore is used, they are not distinguished in template names, but they are in parameter names; the templates P{{NAMESPACE}}, and hence the arrays that call them, use a blank space, where applicable, see e.g. Template:PHelp talk (although {{ns:3}} gives User talk with an underscore, it is anyway not possible to have a parameter name depend on a variable, see below). Capitalization has to be consistent for parameter names as well as inside template names, as a P comes in front.

A disadvantage compared with the system mentioned in the previous section (arrays with a template for each element) is that the absence of a data value shows up as {{{index }}}, as opposed to presenting a link to fill in the value. Referring to a defined array element without the auxiliary Template:Pindex being defined, a link to that template does show up, allowing this to be easily fixed.


One application is a template that contains translations of a particular term, where the indexes are the language codes.

Example 1:

Template:Book contains

Template:Pfr contains Template:Pfr, etc.

{{book|fr}} gives Template:Book.

Example 2:

Template:Nsnp (namespace number, with parameter) contains

  • [[:Template:Nsnp]]

Template:PUser contains Template:PUser, etc.

{{nsnp|namespace=User}} gives Template:Nsnp.

{{nsnp|{{NAMESPACE}}}} gives Template:Nsnp.

Compare Template:Nsn (namespace number), which contains

  • [[:Template:Nsn]]

{{nsn}} gives Template:Nsn (the same).

[edit] A 2D array with each row (or column) contained in a template

A collection of templates can be used as a 2D array: each row is contained in a template, and the template names consist of a common part (which can be considered the name of the 2D array) and a row index.

Thus an array element is referred to by {{2D-array-name index1 |index2 }}

Example: 2D array N

Template:Nlanguage-code contains the word for various languages, expressed in one language.

Template:N de contains

  • [[:Template:N de]]

{{n de|fr}} gives Template:N de.

The choice of what is put together in one template (a row or a column of a given matrix) can either be based on whether {{n de|fr}} or {{n fr|de}} is a more natural notation for this word, or, as has been done here, on what is more practical in filling the templates with data.

[edit] A 2D array contained in a single template

A 2D array can be contained in a single template if a composite index of the form index1 separator index2 is used.

Thus an array element is referred to by {{2D-array-name |index1 separator index2 }}

The same remarks as above apply for the separator.

Example: Template:Ln contains

  • [[:Template:Ln]]

{{ln|de fr}} gives Template:Ln.

[edit] Arrays of higher dimensions

An 3D array can have in each template a single element, a 1D array, a 2D array, or the whole 3D array; correspondingly an element is referred to in one of the folowing ways:

  • {{3D-array-name index1 separator1 index2 separator2 index3 }}
  • {{3D-array-name index1 separator index2 |index3 }}
  • {{3D-array-name index1 |index2 separator index3 }}
  • {{3D-array-name |index1 separator1 index2 separator2 index3 }}

An example of a 4D array is at Commons, with elements of the form Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}} ({{{lang}}}), each in a separate template, such as commons:Template:Potd/2005-06-2 (de). Most of the templates on the project are in this array (see list).

Correspondingly there is a 3D array of image names of the form Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}} such as commons:Template:Potd/2005-06-2 and a corresponding set of images of the form [[Image:{{Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}}}}. The latter can also be considered a 3D array, but with the restriction that elements can only be accessed as such from the project itself (the elements can be accessed, and from Wikimedia projects be embedded, e.g. File:Cataratas027.jpg, but the key to them in terms of the indexes can only be accessed, as the link above shows, but not automatically used).

[edit] Other ways of organizing arrays for easy access

Sections and anchors can be used, if they are systematically named. This only allows separate viewing, not inserting array elements in other texts.


  • Pages for each month with an overview of the 2D sub-array of the 4D array on Commons, together with the corresponding images, in combination with section linking, provide direct access to the overview of the 1D sub-array and the image for a given day, for today [[commons:Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}#{{CURRENTDAY}}]] giving commons:Template:Potd/2022-01#18.

[edit] Naming conventions of array templates

It is not obvious whether a singular or plural name should be used if a single template contains several data, but they are retrieved one at the time, e.g. the template Book above could be called "Template:Translation of "book"" or "Template:Translations of "book"". Similarly Template:Name of talk namespace could also be called Template:Names of talk namespaces.

Since editing a template in general can be considered a little more advanced than just using it by putting a template tag in a page, it can be argued that the naming can best be done from the point of view of people doing the latter, i.e. singular, just like in the case of a template for each array element: {{Translation of "book"|de}} just like the alternative {{Translation of "book" - de}}.

[edit] In-page array content

Instead of having a separate template to define the content of an array, the content of one or more arrays can be put in a template (or, if the array elements are selected based on a variable, it may also be a regular page) between other text.

In this case we have

  • {{pindex |value-1-of-index =value-of-array-element-1 |value-2-of-index =value-of-array-element-2 |..}}

or if index values 1,2,3,.. are used, simply

  • {{pindex |value-of-array-element-1 |value-of-array-element-2 |..}}

where index is an expression in terms of one or more parameters or variables.

Several arrays may use the same index value, to have corresponding selections of array elements, or different ones, e.g. to allow all combinations. An example of the former follows.

Template:Creature demo contains:

[[:Template:Creature demo]]


[edit] Optional text

Due to the blank Template:X0 ("do 0 times"), in each selection an additional option is using index "0", giving no text. This allows putting a text which is optional, 1=on, 0=off.

(Note that a blank page cannot be created directly; first create a non-blank page, e.g. with just one character, then edit the page to blank it. )

[edit] Example

Template:Variable text demo 1 contains

  • [[:Template:Variable text demo 1]]

{{Variable text demo 1|0}} gives

{{Variable text demo 1|1}} gives

[edit] Example with multiple similar lines

{{variable text demo|1}} gives the long version of a text: Template:Variable text demo

while {{variable text demo|0}} gives the short version: Template:Variable text demo

This construction uses the following templates:

  • Template:Variable text demo (see wikitext) - a template that produces a list, with, depending on a parameter ("comprehensiveness toggle"), either long versions of all items, or short versions; it contains all data that is not common to all lines
  • Template:Variable line demo - defines the pattern of a list item, including which parts are only in the full version and which also in the short version; it contains the text that is common to all lines

[edit] Comparison of methods

The method referred to as "array technique using a small auxiliary template" requires one template for each array and just a small one for general use. This makes copying to another project much easier than other methods, especially if no export/import feature is available.

The "method of erasing specification for erasure" requires three templates, one with the data, one depending on the index values used, and a general one.

The other methods require a separate auxiliary template for each value of the index used, or even a template for each element of each array.

[edit] See also


Personal tools