Lua Study Notes --- tables (arrays), lua Study Notes --- Arrays

Source: Internet
Author: User

Lua Study Notes --- tables (arrays), lua Study Notes --- Arrays

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio> T = {}> T[0] = 0> T[1] = 1> for i = 0,1,1 do print(T[i]) end01>

The subscript can be a string,

> T ["h1"] ={}> T ["h1"] ["name"] = "jarlen"> T ["h1"] ["age"] = 26> print (T ["h1"]) table: 003BD180 -- cannot be referenced directly. This is the address of T ["h1"]> print (T ["h1"] ["name"]). jarlen> print (T ["h1"] ["age"]) 26 when the subscript index is a string, you can also write the following:> print (T. h1.name) jarlen> print (T. h1.age) 26
> T.h1.age = 28> print(T.h1.age)28> T.h1.name = "ja"> print(T.h1.name)ja

2> You can also create and initialize data.

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio> T = {1,2}> print(T[1])1> print(T[0])nil> print(T[2])2>

It can be seen that T starts from "1" except for the string subscript.

> T2 = {name = "T2",age = 2}> print(T2.name)T2> print(T2.age)2

For convenience, we can write all the data in the table when defining the table.

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio> T = {>> 14,>> jarlen = {name = "jarlen",age = 26},>> name = "haha"}> print(T.jarlen.name)jarlen> print(T[1])14> print(T["name"])haha> print(T["jarlen"]["name"])jarlen> print(T["jarlen"]["age"])26>

Another example:

> T3 = {>> 14,>> ["jarlen"] = {["name"] = "jarlen",["age"] = 26},>> ["name"] = "haha",>> 20}> print(T3[1])14> print(T3[2])20> print(T3.jarlen.name)jarlen> print(T3.jarlen.age)26

Note:

1> all elements must be separated by commas;

2> the element of an element digit. The subscript starts from the first digit element and is "1". The subsequent digits are arranged in this order.

3> All index values must be enclosed by "[" and "]". If the index value is a string, you can remove the quotation marks and brackets.

It looks like a configuration file and is easy to understand.

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.