Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226825
  • 博文数量: 56
  • 博客积分: 2480
  • 博客等级: 大尉
  • 技术积分: 475
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-28 10:57
文章分类

全部博文(56)

文章存档

2012年(36)

2011年(4)

2010年(2)

2009年(14)

我的朋友

分类: Python/Ruby

2012-02-28 13:21:35

小程序+输出说明

 

 

 

[python] view plaincopy
  1. --1. 逻辑比较 >,>=,<,<=, ==, ~=  
  2. print"---------1----------------")  
  3. print(3.14 ~= math.pi)      --> true  
  4.   
  5. x = x or math.pi            -- 等价于 if not x then x = v end  
  6. print(x)  
  7.   
  8. --2. table 初始化  
  9.   
  10. print"---------2----------------")  
  11. t1 = { color = "blue", own="ha""one""two""three", [4] = "four" }  
  12. print(t1.color)  
  13. print(#t1)  
  14. print(t1[1])  
  15.   
  16.   
  17. --3 循环语句中的局部变量作用域  
  18. print"---------3----------------")  
  19. repeat  
  20.     local line = math.random(110)  
  21.     print( line )  
  22. until line >= 8          --> 在循环条件中可以访问局部变量line  
  23.   
  24. --4 table 的遍历  
  25. print"---------4----------------")  
  26. for i in pairs(t1) do           --> 关联表数组全部打印  
  27.     print(i, t1[i])  
  28. end  
  29.   
  30. print("----")  
  31. for i, v in ipairs(t1) do       --> 只打印数组  
  32.     print( i, v )  
  33. end  
  34.   
  35. print("---")  
  36. t2 = {  
  37.     {name = "tafjk", ip = "192.168.8.1"},  
  38.     {name = "test", ip = "192.168.1.5"},  
  39.     {name = "my", ip = "192.168.201.1"},  
  40. }  
  41.   
  42. -- table sort  
  43.   
  44. for i in pairs(t2) do           --> 关联表数组全部打印  
  45.     print(i, t2[i].name, t2[i].ip)  
  46. end  
  47. print"-" )  
  48. table.sort(t2, function (obj1,obj2) return (obj1.ip > obj2.ip ) end )  
  49. for i in pairs(t2) do           --> 关联表数组全部打印  
  50.     print(i, t2[i].name, t2[i].ip)  
  51. end  
  52.   
  53. --5 函数返回值  
  54. --若函数调用时最后一个参数,则会返回所有返回值,否则,只返回一个值  
  55. print"---------5----------------")  
  56. function foo()  
  57.     return 20091  
  58. end  
  59.   
  60. print( foo() )              --> 2009 1  
  61. print( ( foo() ) )          --> 2009  
  62. x = foo()  
  63. print( x )                  --> 2009  
  64.   
  65. x,y,z = foo(), "foo"        --> 2009 foo nil  
  66. print(x, y, z)  
  67.   
  68. print( unpack( { foo() } ) )    --> 2009 1,  unpack接受list参数,并从下标1开始返回  
  69.   
  70. print( string.format("%d-%d-%s"20091"lua") )  --> 2009-1-lua  

阅读(976) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~