Lua文法入門

3953 ワード

に値を付ける
lua付与は複数の値の同時付与をサポートする
a = 1
b = "foo"
print(a, b)
$ ./lua53.exe hello.lua
1       foo

a, b = 1, 2
print(a, b)
$ ./lua53.exe hello.lua
1       2

a, b = 1
print(a, b)
$ ./lua53.exe hello.lua
1       nil

a, b= 1, 2, 3
print(a, b)
$ ./lua53.exe hello.lua
1       2

a, b = 1, 2
a, b = b, a
print(a, b)
$ ./lua53.exe hello.lua
2       1


table
lua tableのkeyは数字でも文字列でもよく、keyのインデックスは1から始まる
  • tableのデジタルkey
  • t1 = {}
    t1[1] = 10
    t1[2] = "foo"
    print(t1[1],t1[2],t1[3])
    
    t2 = {
        20,
        "bar"
    }
    print(t2[1], t2[2])
    
    $ ./lua53.exe hello.lua
    10      foo     nil
    20      bar
    
  • tableの文字列key
  • t3 = {}
    t3["name"] = "foo"
    print(t3.name)
    $ ./lua53.exe hello.lua
    foo
    
    t4 = {
        ["name"] = "bar",
        ["age"] = 20,
        [3] = false,
    }
    print(t4.name, t4.age, t4[3])
    
    $ ./lua53.exe hello.lua
    bar     20      false
    

    関数#カンスウ#
    lua関数はfunctionをキーワードとし,endを終端とする.関数は複数の値を返すか、変数に値を割り当てることができます.
    function add( a, b )
        return a + b
    end
    
    result = add(1 ,"2")
    print(add(1, 2), result)
    $ ./lua53.exe hello.lua
    3       3.0
    
    sub = function ( x, y )
        return x - y 
    end
    print(sub(2, 1))
    $ ./lua53.exe hello.lua
    1
    
    function add_sub( x, y )
        return x + y, x - y
    end
    print(add_sub(1, 2))
    $ ./lua53.exe hello.lua
    3       -1
    
    

    式#シキ#
    文字列用..接続、localでローカル変数を宣言する
    print(1 + 2)
    print(true and false)
    print(true or false)
    print(not false)
    print("foo" .. "bar")
    $ ./lua53.exe hello.lua
    3
    false
    true
    true
    foobar
    
    function foo( ... )
        x = 1
        local y = 2    --           
    end
    foo()
    print(x)
    print(y)
    $ ./lua53.exe hello.lua
    1
    nil
    
    

    じょうけんステートメント
    if elseif else end
    x, y, z = 10, 20, 30
    if x > 10 then
        print(x)
    elseif y > 20 then
        print(y)
    else
        print(x, y ,z)
    end
    $ ./lua53.exe hello.lua
    10      20      30
    

    ループステートメント
    while do end; for do end; for in do end;
    local i = 0
    while i < 3 do
        print(i)
        i = i + 1
    end
    $ ./lua53.exe hello.lua
    0
    1
    2
    
    for i = 0, 3 do
        print(i)
    end
    print('-----')
    for i = 0, 3, 2 do
        print(i)
    end
    print('*****')
    for i = 3, 0, -1 do
        print(i)
    end
    $ ./lua53.exe hello.lua
    0
    1
    2
    3
    -----
    0
    2
    *****
    3
    2
    1
    0
    
    t = {
        ['name'] = 'foo',
        ['age'] = 10
    }
    for k, v in pairs(t) do
        print(k, v)
    end
    $ ./lua53.exe hello.lua
    name    foo
    age     10
    
    kv = {
        ['name'] = 'foo',
        ['age'] = 10,
        
        [1] = 10,
        [2] = 30,
    }
    
    for k, v in pairs(kv) do 
        print(k, v)
    end
    print('-----')
    for k, v in ipairs(kv) do
        print(k, v)
    end
    
    $ ./lua53.exe hello.lua
    2       30
    age     10
    name    foo
    1       10
    -----
    1       10
    2       30
    
    

    パッケージ
    require
    // foo.lua
    local class = {}
    
    -- class.foo = function ( a, b )
    --  return a + b
    -- end
    
    function class.foo ( a, b )
        return a + b
    end
    
    return class
    
    // hello.lua
    local c = require("foo")
    print(c.foo(1, 2))
    
    $ ./lua53.exe hello.lua
    3
    

    システムライブラリ
    table.insert table.remove # type tonumber tostring string.format
    local t = {}
    for i = 1, 10 do
        table.insert(t, i)
    end
    
    table.remove(t, 2)
    
    for k, v in pairs(t) do
        print(k, v)
    end
    
    $ ./lua53.exe hello.lua
    1       1
    2       3
    3       4
    4       5
    5       6
    6       7
    7       8
    8       9
    9       10
    
    
    local t = {}
    
    t.a = 1
    t.b = 2
    
    t.a = nil
    
    for k, v in pairs(t) do
        print(k, v)
    end
    $ ./lua53.exe hello.lua
    b       2
    
    local t = {5, "1", 3, 4}
    local x = "hello world"
    local y = {1, 2}
    print(#t, #x, #y, type(y))
    print(tostring(t[1]), type(tostring(t[1])), tonumber(t[2]), type(tonumber(t[2])))
    print(string.format("hello %d", y[2]))
    $ ./lua53.exe hello.lua
    4       11      2       table
    5       string  1       number
    hello 2