首页 > 代码库 > lua学习笔记(2)-常用调用
lua学习笔记(2)-常用调用
assert(loadstring("math.max(7,8,9)"))
dofile("scripts/xxx.lua")
math.floor()
math.random() math.random(10, 100)
math.min(3,4,5) math.max(2,3,4)
num = tonumber(str)
str = tostring(num)
len = string.len(str)
str = string.sbu (str, start_position, tail_position)
str = string.format(str, "%s%d", str1, value)
--查找
str_target = "hello" OR pattern = "$%d"
start_postion, tail_position = string.find(str_source, str_target)
str_ret = string.gfind(str_source, pattern)
--替换
str_new = string.gsub(str_source, pattern, str_replace)
table.getn(tab)
--table.sort(tab)
table.insert(tab, position, value)
table.insert(tab, value) --插入到最后
table.remove(tab, position)
table.remove(tab) --删除末尾
pattern:
%a %d %D %l %u %w
paris() & iparis()
tbl = {"alpha", "beta", ["one"] = "uno", ["two"] = "dos"}
for key, value in ipairs(tbl) do
print(key, value)
end
-pairs()函数基本和ipairs()函数用法相同, 区别在于:
pairs()可以遍历整个table,即包括数组及非数组部分。用pairs迭代输出:
-->1 alpha
-->2 beta
-->one uno
-->two dos
ipairs()函数用于遍历table中的数组部分。用ipairs迭代输出:
-->1 alpha
-->2 beta
print("hello world", name) --带换行
io.write("\n")
file = io.open(,)
line_context = file:read()
file:wirte(string.format("%s%s%d"), xxx,xxx,xxx)
file:close()
l = {}
index = 1
for line in file_in:lines() do --read each line
l[index] = tonumber(line)
index = index + 1
end