lua学习笔记(1)-基本语法

==============变量类型
nil
number(实数)    1 2 3.14 7.65e8
string            "hello world" "\n"
boolean(true false)    true false
function
*userdata and threads
table
(1)默认key初始化
t = {a, "hello", b, "world"}

(2)自定义key初始化
t = {n1 = a, n2 = "hello", n3 = b, n4 = "world"}
t.n1 or t[1] or t["n1"]

(3)元素添加赋值
Data = {}
Data.name  = "Thardwick"
Data.class = "Barbarian"
Data.str   = math.random(3, 18)
Data.dex   = math.random(3, 18)
Data[1]
Data.name
Data.["name"]

多维tab
widget = {}
widget.name = {"can opener", "Scissors"}
widget.cost = {"$12.75", "$8.99"}

===============运算符
(实数)算数运算符(字符串转数字)
+
-
*
/
^

字符串运算符(数值转字符串0)
..

关系运算符
==
~=
<
>
<=
>=

逻辑运算符
and
or
not

赋值
a= "hello" .. "world"
n += 1
a, b = b, a
x, y = 1, 2, 3
x, y, z = 1, 2
a, b = func()


=====================if
if xxx then
    xxxx
elseif xxx then
    xxx
else
    xxx
end


======================loop
for index = 1, 10, 1/-1 do
    print(index)
end

index = 1
while index < 10 do
    print(index)
    index = index + 1
end

index = 1
repeat
    print(index)
    index = index + 1
until index > 10


======================function
function max(a, b, c)
    local x = a + b
    return sum, c
end

res1, res2 = foo(1, )

function print_args(...)
    if arg.n <= 0 then0
        return
    end

    for index = 1, arg.n do
        print("arg", index, ":", arg[index])
    end
end

function get_min(tab)
    str = "min = math.min("
    for index, value in ipaies(tab) do
        str = string.format("%s%d%s", str, value, ",")
    end
    
    str = string.sub(str, 1, string.len(str) -1 )
    str = string.format(str, "%s%s", str, ")")
    
    loadstring(str)
    return min
end

posted @ 2014-08-12 17:39  huangshi8421  阅读(174)  评论(0编辑  收藏  举报