Help:Array: Difference between revisions

Content deleted Content added
TRANSWIKI: Copied from Meta:Help:Array.
+ hidden note <!-- -->
 
(14 intermediate revisions by 6 users not shown)
Line 1:
{{soft redirect|m:Help:Array}}
{{H:h|editor toc}}
<!-- DO NOT EDIT -->
 
An example of an array is {{tim|short DOW}}, containing:
----
<pre>{{array
|index={{{1}}}
|1=Mon
|2=Tue
|3=Wed
|4=Thu
|5=Fri
|6=Sat
|0=Sun
|default={{{1}}} is not a valid weekday number.
}}</pre>
----
 
It uses {{tim|array}}, containing <code><nowiki>{{{{{{index}}}|{{{default|}}}}}}</nowiki></code>.
 
Examples:
 
<code><nowiki>{{Short DOW|3}}</nowiki></code> gives <code>{{Short DOW|3}}</code>
 
<code><nowiki>{{Short DOW|{{CURRENTDOW}}}}</nowiki></code> gives <code>{{Short DOW|{{CURRENTDOW}}}}</code>
 
<code><nowiki>{{Short DOW|7}}</nowiki></code> gives <code>{{Short DOW|7}}</code>
 
 
Another version of the two templates could use a term other than "index", e.g. the empty string. However, it should be a term that itself is not used as array index:
 
<code><nowiki>{{t Short DOW|3}}</nowiki></code> using {{tim|t Short DOW}}, containing:
----
<pre>{{array
|{{{1}}}
|1=Mon
|2=Tue
|3=Wed
|4=Thu
|5=Fri
|6=Sat
|0=Sun
|default={{{1}}} is not a valid weekday number.
}}</pre>
---- (hence using implicit name "1" instead of "index") gives "<code>{{T Short DOW|3}}</code>". The implicit 1=3 is overwritten by "1=Mon", and <code>{{{Mon}}}</code> would be produced; since it is undefined, the default is produced, with in this case a confusing error message: not the input is incorrect, but the template itself.
 
See also {{tiw|en|switch}}, where parameter names consist of "case: ", followed by the index.
 
==Foreach==
{{tim|foreach}} allows not only to produce one array element, like above, but also more.
 
Examples:
 
<code><nowiki>{{foreach|call=Short DOW|3}}</nowiki></code> gives "{{foreach|call=Short DOW|3}}"
 
<code><nowiki>{{foreach|call=Short DOW|{{CURRENTDOW}}}}</nowiki></code> gives "{{foreach|call=Short DOW|{{CURRENTDOW}}}}"
 
<code><nowiki>{{foreach|call=Short DOW|7}}</nowiki></code> gives "{{foreach|call=Short DOW|7}}"
 
<code><nowiki>{{foreach|call=Short DOW|3|{{CURRENTDOW}}|7}}</nowiki></code> gives "{{foreach|call=Short DOW|separator=-|3|{{CURRENTDOW}}|7}}"
 
==Other method==
Another method to create a data array using [[Help:Template|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==
<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.
 
{{timcnl|City array demo}} 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}}.
 
==Comparison with other method==
The advantage compared to
[[Help:Template_names%2C_variable_names_and_parameters_depending_on_a_variable_or_parameter#A_1D_array_contained_in_a_template|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.
 
{{h:f|enname=Array}}