欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Lua编程示例(一):select、debug、可变参数、table操作、error

程序员文章站 2022-03-20 22:09:52
function test_print(...) for i=1,select("#",...) do print(i,select(i,...)) e...
function test_print(...)
 for i=1,select("#",...) do
 print(i,select(i,...))
 end
end

test_print(11,12,13,14)


print()
print(debug.traceback())
print()

function test(...)
 for i=1,arg.n do
 print(i.."\t"..arg[i])
 end
end

test("a",2,34,234)
print()
g={}

table.insert(g,{
 name="clairs",
 level = 70,
})
table.insert(g,{
 name="sega",
 level = 35,
})
table.insert(g,{
 name="millber",
 level = 50,
})
function myprint()
 for i,v in ipairs(g) do
 print(i,v["level"],v.name)
 end
end

myprint()
function comp(a,b)
 return a.level<b.level
end
table.sort(g,comp)

print()
myprint()

print()
function foo(str)
 if type(str) ~= "string" then
 error("string error!",2)
 end
end

--foo({x =1 })

tb1={ "asdf","bate","game",one="heihei"}
table.insert(tb1,3,"haha")
table.remove(tb1,2)
for i,v in ipairs(tb1) do
 print(v)
end
print(#tb1)


运行结果为:

1 11 12 13 14
2 12 13 14
3 13 14
4 14

stack traceback:
 my_test.lua:12: in main chunk
 [c]: ?

1 a
2 2
3 34
4 234

1 70 clairs
2 35 sega
3 50 millber

1 35 sega
2 50 millber
3 70 clairs

asdf
haha
game
3