LuaLang Notes

plus2047 于 2021-10-19 发布
-- this is annotation
print("helloWorld")

-- variable ----------------------------------
-- variable is global by default
-- even in a function
str_var = "helloWorld!!!"
local local_var = "local_var" -- a local variable
num_var = 123  -- only one number type: float64
bool_var = true -- or false

nil_var = nil  -- access undefended var will get nil, not error.
table_var = {}  -- empty table.
table_var = {"hello", "world"} -- table[1] == "hello", table[2] == world
table_var = {x="hello", y="world"} -- table["x"] == hello
-- index begin from 1
print(#t) -- size of t

type(num_var) -- return "number" (return type is string)

-- operator ----------------------------------
--: == ~= > < >= <= and or not
"abc" .. "def" == "abcdef" -- .. operator to concat string 
#str -- get size of string
#tab -- get size of table

-- contral -----------------------------------
-- if
if var == nil then
    print('nil')
elseif var == 0 then
    print('0')
else
    print("else")
# end

-- while
while ture or false do
    print("hahaha")
end

for var=1,5,1 do
    print(var)
end
-- get: 1,2,3,4,5

-- for
t = {"A", "B", "C"}
t[5] = 'E'
for k, v in pairs(t) do
    print(v)
end
-- get A B C E
for k, v in pairs(t) do
    print(v)
end
-- get A B C

-- function ----------------------------------
function add(a, b)
    return a + b
    -- support:
    -- return a, b
end

-- string: http://www.runoob.com/lua/lua-strings.html
-- table: http://www.runoob.com/lua/lua-tables.html
-- package: http://www.runoob.com/lua/lua-modules-packages.html