Type of table that Lua learns

Source: Internet
Author: User
Tags constant lua
Relational table type, which is a very powerful type. We can think of this type as an array. Just an array of C languages, which can only be indexed by a positive integer; in Lua, you can use any type of valueTo count the index of the group , but this value cannot beNil. Similarly, in C, the contents of an array allow only one type, and in Lua you can count the contents of a group with any type of value, nil. Basic Introduction
Note three points:
First, all elements are always separated by commas ",";
Second, all index values need to be enclosed in "[" and "]", and in the case of strings, you can also remove quotation marks and brackets, i.e., if there is no [] enclosed, it is considered a string index
Thirdly, if the index is not written, the index is considered to be a number, and is automatically compiled from 1 sequentially;
For example: TT = {"Hello" ,33 }Value =4 tab = {[Tt ] ="Table" ,ie =Value , ["Flag" ] = Nil ,11 }
Print (tab [Tt ]) Print (Tab.key ) Print (tab [1 ])
All of the above are right.
look = {[www] = "OK"} This is not true, www is not assigned, so the default is nil so error table index is nil
---temp = 1 tab = {[Temp] = 1, one} print (Tab[temp])-the result is 11 because 11 does not have an explicit key, so starting with 1, if previously defined, overwrites its value
---temp = 2 tab = {[Temp] = 1, one} temp = 1 print (tab[temp])--the result is 11, although the definition is [temp] = 1, but then we change the value of temp, so we point to another key

The above: 1. For strings, the {} definition can be either key = value or ["flag"] = nil, the index is of type string, and for non-nil types variables (including strings), all can be [Variable]=value way 2. When you use table, you can access it by using the. or [] method for the string. TAB[A],TAB[B], as long as a==b so tab[a] can access the value of Tab[b]3. Regardless of whether the index is defined with a constant or a variable, the value in the table index key is constant, the key of the value does not change with the variable
Nesting TB11 = {Tb12 = {bool = true }}--Simple, it's a table in a table:)--Call magic! Print (Tb11.tb12.bool )--works fine, since it's calling the key and value correctly. Print (Tab11 ["TB12" ].bool )--same as Line 33 Print (Tab11.tb12 ["BOOL" ])--same as Line 33 Print (Tab11 ["TB12" ]["BOOL" ])--same as Line 33
Modify table's value--altering a table ' s content. Basically manipulating the values of the keys. Lucky = {John ="Chips" ,Jane ="Lemonade" ,Jolene ="Egg Salad" }
Lucky.jolene ="Fruit Salad"--changed the value to "fruit salad" instead of "egg salad" lucky.jerry ="Fagaso Food" – adding a new key-value pair to the container lucky. Lucky.john = Nil--Remove John from giving anything or from being a key.
Variability of Table
A = {};B =A ; Print (A ==B )--True
C ,D = {},{};
Print (C ==D )-->false

The table library function uses ----------------------------------------------------------- 1. Table.sort (table [, comp]) sorts table elements in a given order, In-place, from table[1] to Table[n], where n is the length of the table. If comp is given, then it must being a function that receives the table elements, and returns True when the first was less tha n the second (so, not comp (A[i+1],a[i]) would be true after the sort). If comp is isn't given, then the standard Lua operator < is used instead.

the sort algorithm is isn't stable; that's, elements considered equal by the given order could have their relative positi ONS changed by the sort. Name = {"You" ,"Me" , "him","Bill" } --table.sort-only works with arrays! Table.sort (name) for K, <

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.