本文共 1917 字,大约阅读时间需要 6 分钟。
print(math.pi)-->π
print(math.huge)-->Lua中表示的最大数字--[[
3.1415926535898
1.#INF]]
print(math.rad(90))-->转换成弧度
print(math.deg(math.pi))-->转换成角度math.random()用于生成伪随机数
不带参数,它将返回【0, 1]内均匀分布的伪随机实数
带参数n(整数),它将返回【1, n】内均匀分布的伪随机整数
带参数m,n(整数),它将返回【m, n】内均匀分布的伪随机整数
math.randomseed用于设置伪随机数生成器的种子数
math.randomseed(os.time())
math.randomseed(os.time())for i=1,5 do print(math.random(6))end
arr = {}for i=1,10 do arr[i] = i * 10endprint("the length of arr is " .. #arr)for i,v in ipairs(arr) do print("arr[" .. i .. "] = " .. v)end
lines = {luaH_set = 10, luaH_get = 24, luaH_present = 48}for i,v in pairs(lines) do print(i, v)end
arr = {}for i=1,10 do arr[#arr + 1] = i * 10endprint(table.concat(arr, " : ", 1, 10))遍历嵌套数组
function printer(arr) if type(arr) ~= "table" then print(arr) return end for i=1,#arr do printer(arr[i]) endends = { {"a", "nice"}, "and", {"long"}, {"day"}}--printer(s)printer(s)
function printer(arr) if type(arr) ~= "table" then return arr end local res = {} for i=1,#arr do res[i] = printer(arr[i]) end return table.concat(res, " ")endlocal s = { {"a", "nice"}, "and", {"long"}, {"day"}}print(printer(s))排序
lines = { luaH_set = 10, luaH_get = 24, luaH_present = 48, }arr = {}for key in pairs(lines) do arr[#arr + 1] = keyendprint(unpack(arr))table.sort(arr)print(unpack(arr))
转载地址:http://eifhj.baihongyu.com/