Template:Array

From Moegirlpedia
Revision as of 08:21, 10 January 2021 by GuoPC (talk | contribs)
Jump to: navigation, search
Template-info.svg Template Documentation  [View] [Edit] [History] [Refresh]

Introduction

This is a template for generating structured data in the form of an analogue array, which can store, operate or transfer abundant data, and obtain or construct in Lua. In fact, it is to declare more than one name-associated variables at a time.

It is much safer to store and transfer data than applying string processing to split parameters.

Usage

Hereinafter, the form of simulating an array with multiple variables is called a "variable group".

As a variable group of a member of it, the variable name is prefixed with "@array-innerArrayIdentifier:". Usually you don't need to pay attention to this when using, here is just an explanation.

Constructing a variable group

{{array|new|name=Name of a variable group, required
|data1
|data2
|data...
|{{array|in         <!-- You can use "in" to continue adding variable groups inside the template to build a multidimensional variable group -->
  |data...
  |{{array|in
    |data...
   }}
 }}

|r=If "r" is "true", the value of the parameter "name" is returned}}

Value and setting

get

{{array|get|variable group name|the first dimension number|the second dimension number|the third dimension number|...}}
{{array|get|variable group name|count}}
{{array|get|variable group name|the first dimension number|count}}
{{array|get|variable group name|the first dimension number|...the nth dimension number|count}}

"get" can get the value of the variable group member, but if you try to get the variable group index, it would fail and report an error.

set

{{array|set|variable group name|the first dimension number|the second dimension number|...|val=value to set}}

"set" can get the value of the variable group member, but if you try to assign a value to the variable group index, it would fail and report an error.

Other methods

push

{{array|push|variable group name|the first dimension number|the second dimension number|...|val=value to append}}

"push" can append a value at the end of the variable group, and the method of locating the variable group to be operated is the same as "get".

If r=true, then the length of the variable group after appending is returned.

pop

{{array|pop|variable group name|the first dimension number|the second dimension number|...}}

"pop" can remove the last value of the variable group. If the length of the variable group is 0, then nothing would happen.

If r=true, then the deleted value is returned.

unshift

{{array|unshift|variable group name|the first dimension number|the second dimension number|...|val=value to append}}

"unshift" can append a value at the head of the variable group.

If r=true, then the length of the variable group after appending is returned.

shift

{{array|shift|variable group name|the first dimension number|the second dimension number|...}}

"shift" can remove the first value of the variable group. If the length of the variable group is 0, then nothing would happen.

If r=true, then the deleted value is returned.

getIndex

{{array|getIndex|variable group name|the first dimension number|the second dimension number|...}}

"getIndex" can get the index value of the variable group. Ordinary values can also be obtained, but it is not recommended because of no prompt.

ifIndex

{{array|ifIndex|variable group index}}

"ifIndex" can judge whether a string is a variable group index or not. If yes, return 1; if no, return null.

splice

{{array|splice
|index= variable group index
|start= the starting position of the operation, which is 1 by default, cannot exceed the length of the variable group to be operated
|howmany= the number of members to be deleted, which is 1 by default
|value to add...
}}

"splice" can operate the value of variable group, you can use this method to perform operations like deleting, appending and replacing. You can think of this method as the splice method of arrays in JavaScript.

If r=true, then the length of the new variable group is returned.

print

{{array|print|variable group name|the first dimension number|the second dimension number|...}}

"print" can print the variable group to check the content of the variable group.

Simplification

Simplification when constructing

You can omit "new" when constructing. At this time, the first value of the variable group must not be equal to any one of the methods listed above (including new), otherwise an error would occur.

Other

The parameter "name" can also be typed as "id".

Obtain or construct in Lua

See Module:var-array.

Examples

Example 1

{{array|new|name=arr
|ab
|cd
|ef
}}
{{array|get|arr|2}}

Result: cd

Example 2

{{array|id=student
|{{array|in|shinobu|Shinobu Ōmiya|Kichiku Kokeshi}}
|{{array|in|alice|Arisu}}
}}
{{array|get|student|1|1}}、{{array|get|student|2|2}}

Total data amount of student1: {{array|get|student|1|count}}

Try to take the value of internal variable group 1: {{array|get|student|1}}

Result: shinobu、Arisu

Total data amount of student1: 3

Try to take the value of internal variable group 1: Lua error: Cannot get index value for variable group.

Example 3

{{array|id=student
|{{array|in|shinobu|Shinobu Ōmiya|
  {{array|in
  |Short hair|Yamato Nadeshiko
  }}
 }}
}}
{{array|get|student|1|3|2}}

Result: Yamato Nadeshiko

Example 4

{{array|id=test
|111
|222
|333
|444
|555
}}
{{array|splice
|index= {{array|getIndex|test}}
|start=2
|howmany= 3
|aaa|bbb
}}
{{array|print|test}}

Result:

table#1 {
  "111",
  "aaa",
  "bbb",
  "555",
}