Jump to content

Help:Array

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Uncle G's 'bot (talk | contribs) at 21:51, 24 November 2005 (TRANSWIKI: Copied from Meta:Help:Array.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Template:H:hns

A method to create a data array using templates is as follows. 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 "<includeonly>{{safesubst:<noinclude />#invoke:ISO 639 name|{{{fn|iso_639_code_to_name}}}}}</includeonly><noinclude> {{Documentation}} </noinclude>"

calls the "data template"

Template:n en alt containing "[[:Template:N en alt]]"

which calls the "concatenation template"

Template:concatlang containing "[[:Template:Concatlang]]"

A 24 by 60 array

A concatenation template for general use, with unnamed parameters 1 - 1440, is m:Template:concat1440 (backlinks edit).

The root template m:Template:Array1440 (backlinks edit) 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: m:Template:Array60 (backlinks edit).

A matching example data template is m:Template:Array1440 data demo (backlinks edit). 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:Timcnl 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 "[[w:List of people by name: {{{1}}}{{{2}}}|{{{1}}}{{{2}}}]]<noinclude>[[Category:Help:Templates templates]]</noinclude>" gives [[w:List of people by name: Template:Array60Template:Array60|Template:Array60Template: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

{{array|square|3}} using Template:Timcnl and Template:Timcnl and m:Template:concat1440 (backlinks edit) gives void:notfound square.

Nested:

{{array|square|{{array|square|2}}}} gives void:notfound square.

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

Other example

{{array|array demo|3}} using Template:Timcnl and Template:Timcnl and m:Template:concat1440 (backlinks edit) gives void:notfound array demo.

Comparison with other method

The advantage compared to one of the other methods is that unlike that method, no separate auxiliary template is needed for each value of the index used. This makes copying to another project much easier, especially if no export/import feature is available.

On the other hand, in the method on this page enlarging the array, when no preparation for that has been made in advance, requires updating three templates, while the other method requires updating one and creating one.

Template:H:f