lua study notes 2 - table (unfinished)

lua is a table in the "data / code structure" can be used to create two different "data type"
lua language array is actually a table type

array = {1, 2, 3, 4, 5}
print(type(array))    --table

Use basic table:
1. Initialize table
  table table = {}

myTable = {}

2. assignment table to
  array mode: The mode assignment target angle, an index from the start
  key on the way

myTable[1] = "baidu"
myTable[2] = "taobao"
myTable[3] = "jd"
myTable["a"] = "asd"
myTable["xxx"] = "xxx"

Iterator traverse a table: If an array is used ipairs, if the key-value pairs, with pairs
for Key, in ipairs value (table name) do
  body of code
end

for key,value in pairs(myTable) do
  print(key,value)
end

The method of correlation table
1. adding elements table.insert (table name, [position], value)
  to the specified location to increase the element, if the element is not written, and finally a default position increases to
  the right for "array", is not suitable for "key of "
  key-value pair is used: table [ 'key'] = value added embodiment
2. remove elements table.remove (table name, [position])
  if outside the range, does not remove any given element
  that right for "array", it can not be used to "key on"
  key pair is used: table [ 'key'] = nil manner to remove
3. Access table.getn length (table)
  in this way for "array" It can not be used "key to"

table.insert(myTable, 1, "toutiao")
table.insert(myTable, "360")
myTable["abc"] = "abc"

table.remove(myTable)
myTable["xxx"] = nil

for key,value in pairs(myTable) do
    print(key,value)
end

 

Guess you like

Origin www.cnblogs.com/xianguoguo/p/11404559.html