Open main menu

lensowiki β

Changes

Help:Array (older methods)

19,157 bytes added, 21:31, 29 June 2006
Other ways of organizing arrays for easy access: rm link to page with bogus title
{{H:h|editor toc}}
This page is an extension of [[Help:Array]] and explains various older 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.

==Method of erasing specification for erasure==
This method of creating a data array using [[Help:Template|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===
<nowiki>{{</nowiki>langname|de}} gives {{langname|de}}:

The "root template"

{{timc|langname}}

calls the "data template"

{{timc|n en alt}}

which calls the "concatenation template"

{{timc|concatlang}}

===A 24 by 60 array===
A concatenation template for general use, with unnamed parameters 1 - 1440, is {{tim|concat1440}}.

The root template {{tim|Array1440}} 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 <nowiki>{{</nowiki>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: {{tim|Array60}}.

A matching example data template is {{tim|Array1440 data demo}}. The first part contains 1440 pieces of code of the form "5=abc". The second part contains 1440 pieces of code of the form "{{{00:04|}}}=". This part can be reused for other data arrays with these index values.

{{tim|City array demo}}, containing:
----
<pre>{{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}}}=
|{{{00:10}}}=|{{{00:11}}}=|{{{00:12}}}=|{{{00:13}}}=|{{{00:14}}}=|{{{00:15}}}=|{{{00:16}}}=|{{{00:17}}}=|{{{00:18}}}=|{{{00:19}}}=
|{{{00:20}}}=|{{{00:21}}}=|{{{00:22}}}=|{{{00:23}}}=|{{{00:24}}}=|{{{00:25}}}=}}</pre>
---- 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:

<nowiki>{{</nowiki>array1440|city array demo|00:02}} gives {{array1440|city array demo|00:02}}

<nowiki>{{</nowiki>array1440|city array demo|00:03}} gives {{array1440|city array demo|00:03}}

<nowiki>{{</nowiki>array60|city array demo|02}} gives {{array60|city array demo|02}}

Example of using array elements as parameters:

<nowiki>{{t2|{{array60|city array demo|01}}|{{array60|city array demo|02}}}}</nowiki> using {{timc|t2}} gives
{{t2|{{array60|city array demo|01}}|{{array60|city array demo|02}}}}

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

<nowiki>{{array60|city array demo|{{x2|1}}}}</nowiki> gives {{array60|city array demo|{{x2|1}}}}.

<nowiki>{{array60|city array demo|{{array60|city array demo|16}}}}</nowiki> gives {{array60|city array demo|{{array60|city array demo|16}}}}.

===Plain numbers as index values===
<nowiki>{{</nowiki>t array|square|3}} using {{timcnl|t array}} and {{timcnl|square}} and {{tim|concat1440}} gives {{t array|square|3}}.

Nested:

<nowiki>{{t array|square|{{array|square|2}}}}</nowiki> gives {{t array|square|{{t array|square|2}}}}.

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

====Other example====

<nowiki>{{</nowiki>t array|array demo|3}} using {{timcnl|t array}} and {{timcnl|array demo}} and {{tim|concat1440}} gives {{array|array demo|3}}.

==Arrays with a template for each element==

In this method each array element is a template. A 1D [[w:en:array|array]] has elements which are templates with names of the form ''array-name'' ''index'', a 2D array ([[w:en:Matrix (mathematics)|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 are 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 [http://meta.wikimedia.org/w/index.php?title=Special:Allpages/Lang_name&namespace=10 list]; for [[w:en:Main diagonal|diagonal elements]] such as [[Template:Lang name - de (de)]] there are, in addition, copies such as [[Template:Lang name - de]]). See also [[User:Suruena/i18n (en)|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 <nowiki>{{</nowiki>''array-name'' {{{''index''}}}}} using parameter ''index'', or similarly with a variable. Also a page can successively call each array element using <nowiki>{{</nowiki>''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 [[Help:Export and import|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.

Another example:
*See [[w:en:Template talk:Articlespace]]: the namespaces are the index values, the no-talk namespaces are the array values.
*See [[it:Discussioni template:TALKSPACE]]: the namespaces are the index values, the talk namespaces are the array values.

==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:P''value-of-index'' is defined, with the content {{{''value-of-index'' }}} (see [http://meta.wikimedia.org/w/index.php?title=Special:Allpages/p&namespace=10 list for this project] and [http://en.wikipedia.org/w/index.php?title=Special:Allpages/p&namespace=10 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 <nowiki>{{</nowiki>''template name'' |''index'' }}.

Alternatively the parameter <nowiki>{{{</nowiki>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 <nowiki>{{{1}}}</nowiki> 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 <nowiki>P{{NAMESPACE}}</nowiki>, and hence the arrays that call them, use a blank space, where applicable, see e.g. [[Template:PHelp talk]] (although <nowiki>{{ns:3}}</nowiki> gives {{ns:3}} 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:P''index'' 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

*{{msg:Book}}

[[Template:Pfr]] contains {{msg:pfr}}, etc.

<nowiki>{{book|fr}}</nowiki> gives {{book|fr}}.

'''Example 2:'''

[[Template:Nsnp]] (namespace number, with parameter) contains

*{{msgnw:nsnp}}

[[Template:PUser]] contains {{msg:PUser}}, etc.

<nowiki>{{nsnp|namespace=User}}</nowiki> gives {{nsnp|namespace=User}}.

<nowiki>{{nsnp|{{NAMESPACE}}}}</nowiki> gives {{nsnp|namespace={{NAMESPACE}}}}.

Compare [[Template:Nsn]] (namespace number), which contains

*{{msgnw:nsn}}

<nowiki>{{nsn}}</nowiki> gives {{nsn}} (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 <nowiki>{{</nowiki>''2D-array-name'' ''index1'' |''index2'' }}

'''Example: 2D array N'''

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

[[Template:N de]] contains

*{{msgnw:n de}}

<nowiki>{{n de|fr}}</nowiki> gives {{n de|fr}}.

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 <nowiki>{{n de|fr}}</nowiki> or <nowiki>{{n fr|de}}</nowiki> 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 <nowiki>{{</nowiki>''2D-array-name'' |''index1'' ''separator'' ''index2'' }}

The same remarks as above apply for the separator.

Example: [[Template:Ln]] contains
*{{msgnw:ln}}

<nowiki>{{ln|de fr}}</nowiki> gives {{ln|de fr}}.

==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:
*<nowiki>{{</nowiki>''3D-array-name'' ''index1'' ''separator1'' ''index2'' ''separator2'' ''index3'' }}
*<nowiki>{{</nowiki>''3D-array-name'' ''index1'' ''separator'' ''index2'' |''index3'' }}
*<nowiki>{{</nowiki>''3D-array-name'' ''index1'' |''index2'' ''separator'' ''index3'' }}
*<nowiki>{{</nowiki>''3D-array-name'' |''index1'' ''separator1'' ''index2'' ''separator2'' ''index3'' }}

An example of a 4D array is at Commons, with elements of the form <nowiki>Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}} ({{{lang}}})</nowiki>, 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 [http://commons.wikimedia.org/w/index.php?title=Special:Allpages&namespace=10 list]).

Correspondingly there is a 3D array of image names of the form <nowiki>Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}}</nowiki> such as [[commons:Template:Potd/2005-06-2]] and a corresponding set of images of the form <nowiki>[[Image:{{Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}}}}</nowiki>. 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 <nowiki>[[commons:Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}#{{CURRENTDAY}}]]</nowiki> giving [[commons:Template:Potd/{{CURRENTYEAR}}-{{CURRENTMONTH}}#{{CURRENTDAY}}]].

==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: <nowiki>{{Translation of "book"|de}} just like the alternative {{Translation of "book" - de}}</nowiki>.

==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
*<nowiki>{{</nowiki>p''index'' |''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
*<nowiki>{{</nowiki>p''index'' |''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:
:<code>{{msgnw:creature demo}}</code>

Thus:
*<nowiki>{{creature demo|1}}</nowiki> gives: {{creature demo|1}}
*<nowiki>{{creature demo|2}}</nowiki> gives: {{creature demo|2}}
*<nowiki>{{creature demo|3}}</nowiki> gives: {{creature demo|3}}

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

*{{msgnw:Variable text demo 1}}

<nowiki>{{Variable text demo 1|0}}</nowiki> gives
*{{Variable text demo 1|0}}

<nowiki>{{Variable text demo 1|1}}</nowiki> gives
*{{Variable text demo 1|1}}

====Example with multiple similar lines====

<nowiki>{{variable text demo|1}}</nowiki> gives the long version of a text:
{{variable text demo|1}}

while <nowiki>{{variable text demo|0}}</nowiki> gives the short version:
{{variable text demo|0}}

This construction uses the following templates:
*[[Template:Variable text demo]] (see [http://meta.wikimedia.org/w/index.php?title=Template:Variable_text_demo&action=edit 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.

==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
*{{tiw|en|Locale length}} - this template and some of its older versions apply several array techniques; see also [[w:en:Wikipedia_talk:No_meta-templates#Conversion_Template]].

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