Help:Array (older methods)

From The Scuba Wiki

Jump to: navigation, search
MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins +/-

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.

Contents

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

{{langname|de}} gives German:

The "root template"

Template:langname containing "{{n en alt|de=de|el=el|en=en|{{{1}}}=}}"

calls the "data template"

Template:n en alt containing "{{concatlang|de=German|el=Greek|en=English|{{{de}}}=|{{{el}}}=|{{{en}}}=}}"

which calls the "concatenation template"

Template:concatlang containing "{{{de}}}{{{el}}}{{{en}}}"

A 24 by 60 array

A concatenation template for general use, with unnamed parameters 1 - 1440, is template:concat1440 ( talk edit history links ).

The root template template:Array1440 ( talk edit history links ) 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:Array60 ( talk edit history links ).

A matching example data template is template:Array1440 data demo ( talk edit history links ). 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:City array demo ( talk edit history links ), 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}}}=
|{{{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}}}=}}

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:

{{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:t2 containing "parameter 1 is "{{{1}}}", parameter 2 is "{{{2}}}"<noinclude>[[Category:Demo template]]</noinclude>" gives parameter 1 is "Template:Array60", parameter 2 is "Template:Array60"

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.

Plain numbers as index values

{{t array|square|3}} using Template:t array (talk, backlinks, edit) containing:


{{{{{1}}}|1=1|2=2|3=3|4=4|5=5|{{{2}}}=}}


and Template:square (talk, backlinks, edit) containing:

<includeonly>{{#expr:{{{1}}}*{{{1}}}}}</includeonly> <noinclude> Gives the square of a number:

Usage
<tt>&#123;{square| a}}</tt>
Example
<tt>&#123;{square|25}}</tt>
Equals: {{square|25}}

[[Category:Mathematical templates|{{PAGENAME}}]] </noinclude>


and template:concat1440 ( talk edit history links ) gives 1

.

Nested:

{{t array|square|{{array|square|2}}}} gives Expression error: Unexpected * operator .

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

Other example

{{t array|array demo|3}} using Template:t array (talk, backlinks, edit) containing:


{{{{{1}}}|1=1|2=2|3=3|4=4|5=5|{{{2}}}=}}


and Template:array demo (talk, backlinks, edit) containing:

[[:Template:Array demo]]


and template:concat1440 ( talk edit history links ) 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 {{{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.

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 {{{fr}}}, etc.

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

Example 2:

Template:Nsnp (namespace number, with parameter) contains

  • {{P{{{namespace}}}|=0|Talk=1|User=2|User talk=3|Template=10|Template talk=11|Help=12|Help talk=13}}

Template:PUser contains {{{User}}}, etc.

{{nsnp|namespace=User}} gives 2.

{{nsnp|{{NAMESPACE}}}} gives 12.

Compare Template:Nsn (namespace number), which contains

  • {{#switch:{{NAMESPACE}}|=0|Talk=1|User=2|User talk=3|Template=10|Template talk=11|Help=12|Help talk=13|NL Help=116|Overleg help=117}}

{{nsn}} gives 12 (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 {{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

  • {{p{{{1}}}|af=Afrikaans|de=Deutsch|en=English|fr=Französisch|nl=Niederländisch}}

{{n de|fr}} gives Französisch.

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.

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

  • {{#switch:{{{1}}}|de en=German|de de=Deutsch|de nl=Duits|de fr=Allemand|fr en=French|fr de=Französisch|fr nl=Frans|fr fr=Français}}

{{ln|de fr}} gives Allemand.

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

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/{{CURRENTYEAR}}-{{CURRENTMONTH}}#{{CURRENTDAY}}]] giving commons:Template:Potd/2024-04#30.

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

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:

A '''{{p{{{1}}}|centaur|griffin|harpy}}''' is a cross between {{p{{{1}}}|a man|a lion|a woman}} and {{p{{{1}}}|a horse|an eagle|a vulture}}.

Thus:

  • {{creature demo|1}} gives: A centaur is a cross between a man and a horse.
  • {{creature demo|2}} gives: A griffin is a cross between a lion and an eagle.
  • {{creature demo|3}} gives: A harpy is a cross between a woman and a vulture.

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

  • With a speed of 30 m{{x{{{1}}}|etre}}/s{{x{{{1}}}|econd}} a distance of 1800 m{{x{{{1}}}|etres}} is covered in 60 s{{x{{{1}}}|econds}}.

{{Variable text demo 1|0}} gives

  • With a speed of 30 m/s a distance of 1800 m is covered in 60 s.

{{Variable text demo 1|1}} gives

  • With a speed of 30 metre/second a distance of 1800 metres is covered in 60 seconds.

Example with multiple similar lines

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

  • With a speed of 30 metre/second a distance of 600 metres is covered in 20 seconds because 600 / 30 = 20.
  • With a speed of 10 metre/second a distance of 500 metres is covered in 50 seconds because 500 / 10 = 50.

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

  • With a speed of 30 m/s a distance of 600 m is covered in 20 s because 600 / 30 = 20.
  • With a speed of 10 m/s a distance of 500 m is covered in 50 s because 500 / 10 = 50.

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.

See also

Template:Ph:Array (older methods)

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_(older_methods) | 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