The construction of Lua language--table

Source: Internet
Author: User

Table is the most important and powerful weapon in the Lua language, and the simplest constructor is {}. -So when you see {}, you actually call a constructor.

Initialize the array directly:

Days = {"Monday","Tuesday","Wednesday "}

Initialize a table as a record:

Days = {m = "Monday", t = "Tuesday", w= "Wednesday"}
Note that the record is actually equivalent to M,T,W as the following table, so the default table days[0] returns nil

Add Field: You can add any type of field to the table, and the constructor {} only affects initialization.

0 0, label="console"= {sin (0), sin (1), sin (2 )}w[1"another field"= w
field is a key value

Construct a linked list with table:

Nil  for inch Io.lines  Do    = {next = List,value = line} end = while      1  do     Print (l.value)     = L.Nextend

Record and list mashup:

polyline = {color = "blue", thickness = 2,npoints = 4,{x = 0, y = 0},{x = -10, y = 0},{x = -10, y = 1},{x= 0, y = 1}}print ( polyline[1].x)

List and record styles are exceptions to general initialization:

{x=0, y=0}    <-->  {["x"]=0,["Y"]=0}
{"red","green","blue" }   <--> {[1] = "Red", [2] = "Green", [3] = "Blue"}

If you want the array to start with 0: (not recommended)

Days = {[0"Sunday","Monday","Tuesday  ","Wednesday"}

Constructors are optional, convenient for later expansion/, and can be substituted for each other.

The construction of Lua language--table

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.