Lua - The Language 1 控制结构、table数据结构

本文介绍了Lua编程语言的基础知识,包括字符串的特殊引用方式、循环和条件判断结构、简单及嵌套的table数据结构的使用方法,并提供了示例代码。

1。用[[xxxxx]]直接引用字符串,类似于C#里的 @"xxxxx"。
   
比如:                       [[alo          [[
        "alo\n123\""    =   123"]]     =   alo
                                           123"]]

2。循环结构和选择结构
 
-- for and if
for i = 1,5 do
 print("i is now "..i)
    if i < 2 then
     print("small")
    elseif i < 4 then
     print("medium")
    else
     print("big")
    end
end

-- while
i=1
while i <=5 do
 print("i is now "..i)
    if i < 2 then
     print("small")
    elseif i < 4 then
     print("medium")
    else
     print("big")
    end
    i = i+1
end

-- repeat until,注意没有end
i=1
repeat
 print("i is now "..i)
    if i < 2 then
     print("small")
    elseif i < 4 then
     print("medium")
    else
     print("big")
    end
    i = i+1
until i == 6

输出结果:
i is now 1
small
i is now 2
medium
i is now 3
medium
i is now 4
big
i is now 5
big

3。简单的table结构

-- Arrays
myData = {}
myData[0] = "foo"
myData[1] = 42

-- Hash tables
myData["bar"] = "baz"

-- Iterate through the structure
for key,value in myData do
 print(key .. "=" .. value)
end

输出结果:
1=42
0=foo
bar=baz

4。嵌套的table结构

-- table结构
myPolygon = {
 color="blue",
 thickness=2,
 npoints=4;
 {x=0,y=0},
 {x=-10,y=0},
 {x=-5,y=4},
}

-- 索引形式
print(myPolygon["color"])

-- 点形式
print(myPolygon.thickness)

-- 三个子表,分别是myPolygon[1] 到 myPolygon[3]
print(myPolygon[2].x)
print(myPolygon[3]["y"])

输出结果:
blue
2
-10
4

转载于:https://www.cnblogs.com/anf/archive/2006/03/01/340440.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值