Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14867
  • 博文数量: 3
  • 博客积分: 70
  • 博客等级: 民兵
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-05 22:08
文章分类

全部博文(3)

文章存档

2013年(3)

最近访客

分类: PERL

2013-05-01 00:15:09

一、表的构造

(list 风格
    days = { "Sunday" , "Monday" , "Tuesday", "Wednesday", 
         "Thursday" , "Friday" , "Saturday" } 
    Lua 将"Sunday" 初始化days[1](第一个元素索引为 1),用"Monday"初始化days[2]...

    w = {x=0, y=0, label="console"} 
    x = {sin(0), sin(1), sin(2)} 
    w[1] = "another field"  
    x.f = w 
    print(w[ "x" ])   --> 0 
    print(w[1])   --> another field 
    print(x.f[1])   --> another field 
    w.x = nil      -- remove field "x" 

(record 风格
    opnames = {["+" ] = "add" , [ "-" ] = "sub" , 
        ["*" ] = "mul" , [ "/" ] = "div" } 

    i = 20; s = "-"  
    a = {[i+0] = s, [i+1] = s..s, [i+2] = s..s..s} 
 
    print(opnames[s])   --> sub 
    print(a[22])     --> --- 



二、流程控制
(if)
    
if conditions then  
        then-part 
    elseif conditions then 
        elseif-part
    else
        else-part 
    end ;
    
(while)
    while  condition do 
        statements; 
    end ;

(repeat-until)
    repeat 
        statements; 
    until  conditions;

(数值for 循环)
    for  var=exp1,exp2,exp3  do 
         loop-part 
    end 

(范型for 循环)
-- print all values of array 'a'  
    for  i,v  in ipairs(a) do print(v)  end  
-- print all keys of table 't' 
    for  k  in pairs(t)  do print(k)  end

范型for 和数值for 有两点相同: 
1. 控制变量是局部变量 
2. 不要修改控制变量的值


阅读(1021) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:LUA 拾遗(表的构造、流程控制)

给主人留下些什么吧!~~