性能测试之Lua教程
数据类型
base
nil、boolean、number、string、userdata、function、thread和table
function | 由 C 或 Lua 编写的函数 |
---|---|
userdata | 表示任意存储在变量中的C数据结构 |
thread | 表示执行的独立线路,用于执行协同程序 |
table | Lua 中的表(table)其实是一个”关联数组”(associative arrays),数组的索引可以是数字或者是字符串。在 Lua 里,table 的创建是通过”构造表达式”来完成,最简单构造表达式是{},用来创建一个空表。 |
代码示例
--[[
Learn lua
--]]
-------- string ---------
str1 = "aaaaa"
str2 = [[
bb
cc
<ddd>
]]
str3 = str1..str2 -- .. 连接字符串
print(str3)
print("str3.length = "..#str3) -- # 计算string长度
-------- table ---------
local tab1 = {}
local tab2 = {"zhao","qian","sun"}
tab1["name1"] = tab2[1] -- table初始index从1开始,index就是key
tab1["tb2"] = tab2
print(tab1["name1"]) -- table是关联数组,即有顺序的map
for key,value in pairs(tab1) do -- 遍历
if(type(value) == "string")
then print(key.." : "..value) -- table不能直接打印
end
end
-------- function ---------
function printTable( tableVari )
for key,value in pairs(tableVari) do -- pairs
if(type(value) == "string")
then print(key.." : "..value)
else
printTable(value)
end
end
end
print("----print table with function.----")
printTable(tab1)
-- 函数可以作为参数
function log( str, formatFunc )
str = "Error : "..str
formatFunc(str)
end
log("stack over flow",print)
-------- thread ---------
-------- module ---------
module = {}
module.TEMPLATE_NAME = "SYS_SHOP_TEST"
local function checkId( id )
if(id == 0)
then return false
else
return true
end
end
function module.sendMessage( id )
if pcall(checkId(id))
then print("you send message to : "..id)
end
end
module.sendMessage(0)

This work is licensed under a CC A-S 4.0 International License.