Help:Array (older methods)

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.

Method of erasing specification for erasure
This method of creating a data array using templates is not recommended.

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.

Language name example
gives :

The "root template"

calls the "data template"

which calls the "concatenation template"

A 24 by 60 array
A concatenation template for general use, with unnamed parameters 1 - 1440, is.

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

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:.

A matching example data template is. 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.

, containing:

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.

Examples:

gives

gives

gives

Example of using array elements as parameters:

using gives

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

gives.

gives.

Plain numbers as index values
using and  and  gives.

Nested:

gives.

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

Other example
using and  and  gives.

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 (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
 * or if index values 1,2,3,.. are used, simply
 * An array element is referred to as.
 * An array element is referred to as.
 * An array element is referred to as.

Alternatively the parameter 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 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, and hence the arrays that call them, use a blank space, where applicable, see e.g. Template:PHelp talk (although     gives      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, 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.

Translations

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, etc.

gives.

Example 2:

Template:Nsnp (namespace number, with parameter) contains



Template:PUser contains, etc.

gives.

gives.

Compare Template:Nsn (namespace number), which contains



gives (the same).

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

Example: 2D array N

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

Template:N de contains



gives.

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 or  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.

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

The same remarks as above apply for the separator.

Example: Template:Ln contains

gives.

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:

An example of a 4D array is at Commons, with elements of the form Template:Potd/2024-August-27, 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/2024-August-27 such as commons:Template:Potd/2005-06-2 and a corresponding set of images of the form [[Image: . 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. [[Image:Cataratas027.jpg|50px|]], but the key to them in terms of the indexes can only be accessed, as the link above shows, but not automatically used).

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.

Example:


 * 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/2024-August giving commons:Template:Potd/2024-August.

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: just like the alternative.

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 or if index values 1,2,3,.. are used, simply

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:

Thus:
 * gives:
 * gives:
 * gives:

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. )

Example
Template:Variable text demo 1 contains



gives

gives

Example with multiple similar lines
gives the long version of a text:

while gives the short version:

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

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.