脚本专栏 
首页 > 脚本专栏 > 浏览文章

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

(编辑:jimmy 日期: 2025/3/6 浏览:3 次 )
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


上一篇:Lua调用自定义C模块
下一篇:lua闭包的理解以及表与函数的几种表达方法
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap