-
學習查看全部
-
相對與字典,lua的table類型,更像是數組[]
查看全部 -
print("hello world")
查看全部 -
tonumber("123") -- 123 ?數值類型 tostring(131) -- 131 字符串類型 string.format("number 1 to %d",100) -- %d 為后面的100 #table -- table.length #為長度符號 type(a) -- 判斷a的類型 eg:type(a) - string (table...)
查看全部 -
pairs ?遍歷全部
ipairs 遍歷是數字的K
查看全部 -
Table 作為數組的時候 ?下標是從1開始 不是0
查看全部 -
-- 這是注解
a = 1
b = "Hello"
c = true
-- 快速賦值
a,b = 1,2
-- 快速交換值
a,b = b,a;
print(a,b,c)
-- 創建一個變量
-- lua table 數組+映射
-- table 從一開始
d = {}
d[1] = 10
d[2] = 20
d[3] = "ok"
e = {
? ?10,
? ?20,
? ?"ok"
}
f = {}
f["ok"]=1
f[3]=false
g = {
? ["ok"]=2,
? [3]=true
}
print(d[1],d[2],d[3])
print(e[1],e[2],e[3])
print(f.ok,f[3])
print(g.ok,g[3])
-- 函數
function add(a,b)
? ? return a+b
end
function add1(a,b)
-- 多返回值
? ? return a+b,a-b
end
-- 函數也是一種類型
addnumber = function(a,b)
? ?return a+b
end
print(add(1,2))
print(addnumber(1,2))
-- c沒有 ++運算符
-- 邏輯運算
print(true and false)
print(true or false)
print(not true)
-- .. 拼接字符串
print("111".."222")
function foo()
-- 變量的作用域限制在函數內
? local a = 1
end
if false then?
? ?print("if")
else
? ?print("else")
end
if false then?
? ?print("if")
elseif false then
? ?print("1");
elseif false then
? ?print("2")
else
? ?print("3")
end
-- while 循環
local i = 0
while i <10 do
? ?print("while")
? ?print(i)
? ?i=i+1
end
-- for 循環
for i=1,10 do
? print("for")
? print(i)
end
-- 倒序打印
--? 起始值,終值,步長
for i=10,0,-1 do
? print("for")
? print(i)
end
h = {
? ["host"]="127.0.0.1",
? ["user"]="root",
? ["pass"]="root"
}
print("-------")
-- ipairs 只搜索數組?
-- pairs ipairs 迭代器
for k,v in pairs(h) do
? ? ? ?print(k,v)
end
-- require("foo") 只執行一次
-- dofile("foo.lua") 調多少次執行多少次
local c= require("foo")
print(c.foo(1,2))
--dostring("print(\"Hello\")");
table.insert(h,11);
-- #取對象長度
-- type獲得數據類型
-- tonumber("3.14")
-- tostring(3.14)
-- string.format("h1 %d",2)
print(#h)
--nil
--[[
?長注釋
]]
查看全部 -
table = 數組+映射
數組的下標從1開始,連續使用,自動擴展
萬物皆值,函數也是一種值
函數支持多返回參數
邏輯運算就是說英文 ?and 與 or 或 not 非
字符串的鏈接 ?..
local關鍵字 代碼的優化 作用域的控制
迭代器遍歷
數組遍歷 for k,v in ipairs(t) do ? end
Table遍歷 for k, v in pairs(t) do ?end
?
查看全部 -
lua的系統庫
查看全部 -
Lua 語言 for i= 1, 1-10,do Print(i) end --Lua的值和類型 a=1 b=2 print(a,b)查看全部
-
lua: Table 表達式與流程控制 if條件語句 while for 循環語句 包和庫查看全部
-
lua與c速度對比查看全部
-
lua沒有main函數入口查看全部
-
foo為table中的映射(鍵),鍵值為函數查看全部
-
local大法好查看全部
舉報