select、debug、可变参数、table操作、error

  1. function test_print(...)  
  2.     for i=1,select("#",...) do  
  3.     print(i,select(i,...))  
  4.     end  
  5. end  
  6.   
  7. test_print(11,12,13,14)  
  8.   
  9.   
  10. print()  
  11. print(debug.traceback())  
  12. print()  
  13.   
  14. function test(...)  
  15.     for i=1,arg.n do  
  16.         print(i.."\t"..arg[i])  
  17.     end  
  18. end  
  19.   
  20. test("a",2,34,234)  
  21. print()  
  22. g={}  
  23.   
  24. table.insert(g,{  
  25.     name="Clairs",  
  26.     level = 70,  
  27. })  
  28. table.insert(g,{  
  29.     name="SEGA",  
  30.     level = 35,  
  31. })  
  32. table.insert(g,{  
  33.     name="Millber",  
  34.     level = 50,  
  35. })  
  36. function myprint()  
  37.     for i,v in ipairs(g) do  
  38.         print(i,v["level"],v.name)  
  39.     end  
  40. end  
  41.   
  42. myprint()  
  43. function comp(a,b)  
  44.     return a.level<b.level  
  45. end  
  46. table.sort(g,comp)  
  47.   
  48. print()  
  49. myprint()  
  50.   
  51. print()  
  52. function foo(str)  
  53.     if type(str) ~= "string" then  
  54.         error("string error!",2)  
  55.     end  
  56. end  
  57.   
  58. --foo({x =1 })  
  59.   
  60. tb1={ "asdf","bate","game",one="heihei"}  
  61. table.insert(tb1,3,"haha")  
  62. table.remove(tb1,2)  
  63. for i,v in ipairs(tb1) do  
  64.     print(v)  
  65. end  
  66. print(#tb1)  


运行结果为:

 

  1. 1   11  12  13  14  
  2. 2   12  13  14  
  3. 3   13  14  
  4. 4   14  
  5.   
  6. stack traceback:  
  7.     my_test.lua:12: in main chunk  
  8.     [C]: ?  
  9.   
  10. 1   a  
  11. 2   2  
  12. 3   34  
  13. 4   234  
  14.   
  15. 1   70  Clairs  
  16. 2   35  SEGA  
  17. 3   50  Millber  
  18.   
  19. 1   35  SEGA  
  20. 2   50  Millber  
  21. 3   70  Clairs  
  22.   
  23. asdf  
  24. haha  
  25. game  
  26. 3  


 本篇博客出自  阿修罗道,转载请注明出处:http://blog.csdn.net/fansongy/article/details/7005142

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值