Lua table 学习笔记

3.9k 词

Lua table定义

table类型实现了“关联数组”。“关联数组” 是一种具有特殊索引方式的数组,索引可为字符串string或(整)数number类型.

local corp = {
    web = "www.google.com",   --索引为字符串,key = "web", value = "www.google.com"
    telephone = "12345678",   --索引为字符串
    staff = {"Jack", "Scott", "Gary"}, --索引为字符串,值也是一个表
    100876,              --相当于 [1] = 100876,此时索引为数字,key = 1, value = 100876
    100191,              --相当于 [2] = 100191,此时索引为数字
    [10] = 360,          --直接把数字索引给出
    ["city"] = "Beijing" --索引为字符串
}

print(corp.web) –>output:www.google.com
print(corp["telephone"]) –>output:12345678
print(corp[2]) –>output:100191
print(corp["city"]) –>output:"Beijing"
print(corp.staff[1]) –>output:Jack
print(corp[10]) –>output:360

在定义表的时候,我们可以把所有的数据内容一起写在”{“和”}”之间,这样子是非常方便,而且很好看。比如:

T1= { 
    10, -- 相当于 [1] = 10 
    [100] = 40, 
    John= -- 如果你原意,你还可以写成:["John"] = 
    { 
        Age=27, -- 如果你原意,你还可以写成:["Age"] =27 
        Gender=Male -- 如果你原意,你还可以写成:["Gender"] =Male 
    }, 
    20 -- 相当于 [2] = 20 
}

在构造TTable时,可以用;代替,分割数据项。

{x=10, y=45; "one", "two", "three"}

Lua table的遍历

不同于其他语言的数组把 0 作为数组的初始索引,在 Lua 里表的默认初始索引一般以 1 开始. 对table的遍历,用关键字pairs.

tbl = {"apple", "pear", "orange", "grape"}
for key, val in pairs(tbl) do
    print(key, val)
end

–>output:
1 apple
2 pear
3 orange
4 grape

Lua table删除元素

给table 表里的变量赋一个nil值,等同于把它们删掉.示例如下:

tab1 = { key1 = "val1", key2 = "val2", "val3" }
for k, v in pairs(tab1) do
    print(k .. " - " .. v)
end

–> output:
1 - val3
key2 - val2
key1 - val1

tab1.key1 = nil
for k, v in pairs(tab1) do
print(k " - " v)
end

–> output:
1 - val3
key2 - val2

tab1[1] = nil
for k, v in pairs(tab1) do
print(k " - " v)
end

–> output:
key2 - val2

Lua table库介绍

table.concat (table [, sep [, i [, j ] ] ])

对于元素是string或者number类型的表table,返回table[i]..sep..table[i+1] ··· sep..table[j]连接成的字符串。填充字符串sep默认为空白字符串。起始索引位置i默认为1,结束索引位置j默认是table的长度。如果i大于j,返回一个空字符串。

示例代码

a = {1, 3, 5, "hello" }
print(table.concat(a))
print(table.concat(a, "|"))
print(table.concat(a, " ", 4, 2))
print(table.concat(a, " ", 2, 4))

–>output
135hello
1|3|5|hello

3 5 hello

table.insert (table, [pos ,] value)

在表table的pos索引位置插入value,其它元素向后移动到空的地方。pos的默认值是表的长度加一,即默认是插在表的最后。

示例代码

a = {1, 8}             --a[1] = 1,a[2] = 8
table.insert(a, 1, 3)   --在表索引为1处插入3
print(a[1], a[2], a[3])
table.insert(a, 10)    --在表的最后插入10
print(a[1], a[2], a[3], a[4])

–>output
3 1 8
3 1 8 10

table.maxn (table)

返回表table的最大索引编号;如果此表没有正的索引编号,返回0。

示例代码

a = {}
a[-1] = 10
print(table.maxn(a))
a[5] = 10  
print(table.maxn(a))

–>output
0
5

table.remove (table [, pos])

在表table中删除索引为pos(pos只能是number型)的元素,并返回这个被删除的元素,它后面所有元素的索引值都会减一。pos的默认值是表的长度,即默认是删除表的最后一个元素。

示例代码

a = { 1, 2, 3, 4}
print(table.remove(a, 1)) --删除速索引为1的元素
print(a[1], a[2], a[3], a[4])

print(table.remove(a)) 删除最后一个元素
print(a[1], a[2], a[3], a[4])

–>output
1
2 3 4 nil
4
2 3 nil nil

table.sort (table [, comp])

按照给定的比较函数comp给表table排序,也就是从table[1]到table[n],这里n表示table的长度。 比较函数有两个参数,如果希望第一个参数排在第二个的前面,就应该返回true,否则返回false。 如果比较函数comp没有给出,默认从小到大排序。

示例代码

function compare(x, y) --从大到小排序
   return x > y    --如果第一个参数大于第二个就返回true,否则返回false
end

a = { 1, 7, 3, 4, 25}
table.sort(a) 默认从小到大排序
print(a[1], a[2], a[3], a[4], a[5])
table.sort(a, compare) 使用比较函数进行排序
print(a[1], a[2], a[3], a[4], a[5])

–>output
1 3 4 7 25
25 7 4 3 1

table一些有用的函数

local function table_is_empty(t)
    return next(t) == nil
end

local function table_is_array(t)
if type(t) ~= "table" then return false end
local i = 0
for _ in pairs(t) do
i = i + 1
if t[i] == nil then return false end
end
return true
end

local function table_is_map(t)
if type(t) ~= "table" then return false end
for k,_ in pairs(t) do
if type(k) == "number" then return false end
end
return true
end

            <hr/>

            
            
            
            <div class="ds-share" style="text-align: right" data-thread-key="/2015/11/23/lua_table" data-title="Lua table 学习笔记" data-url="http://hooting.github.io/2015/11/23/lua_table/" data-images="http://hooting.github.io/img/post-bg-2015.jpg" data-content="Lua table定义

table类型实现了“关联数组”。“关联数组” 是一种具有特殊索引方式的数组,索引可为字符串string或(整)数number类型… | Hooting Blog ">

            <hr/>
            </div>