local d, f =5, 10--声明局部变量 d, fd, f =5, 10--声明全局变量 d, fd, f =10--声明全局变量 d, f, 其中 f 的值为 nil
如果只是定义了但是没有初始化变量,则静态存储变量被隐式初始化为 nil
1.2 示例
-- 变量定义:local a, b-- 初始化a =10b =30print("value of a:", a)print("value of b:", b)-- 交换变量的值b, a = a, bprint("value of a:", a)print("value of b:", b)f =70.0/3.0print("value of f", f)
value of a: 10
value of b: 30
value of a: 30
value of b: 10
value of f 23.333333333333
g = 20 -- 变量属于左值表达式,合法
10 = 20 -- 非法
-- lua 中允许一个赋值语句出现多个左值表达式和右值表达式
g, l = 20, 30
print(type("This is a string")) -- string
local t = 10
print(type(5.8 * 10)) -- number
print(type(true)) -- boolean
print(type(print)) -- function
print(type(type)) -- function
print(type(nil)) -- nil
print(type(type(ABC))) -- string
local a, b = 10, 20
print(a ^ 2) -- 100.0 幂操作符
print(-a) -- -10 一元减操作符用于取反
while (condition) do
statement(s)
end
-- eg:
local a = 1;
while a < 5 do
print(a)
a = a + 1
end
for init, max/min value, increment do
statement(s)
end
-- eg:
for i = 1, 10, 1 do
print(i)
end
for i = 10, 1, -1 do
print(i)
end
for (int i = 1; i <= 10; i++) {
std::cout << i << std::endl;
}
for (int i = 10; i >= 1; i--) {
std::cout << i << std::endl;
}
repeat
statement(s)
until (condition)
-- eg: 其实就是相当于其他语言的 do while
local a = 1
repeat
print(a)
a = a - 1
until (a < 1)
if (boolean_expression) then
statement(s)
end
optional_function_scope function function_name( argument1, argument2, argument3..., argumentn)
function_body
return result_params_comma_separated
end
function average(...)
local result = 0
local arg = { ... }
for i, v in ipairs(arg) do
result = result + v
end
return result / #arg
end
print(average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) -- 5.5
print(string.format("%s, %s", "hello", "world")) -- 字符串格式化 hello, world
date = 2; month = 1; year = 2014
print(string.format("Date formatting %02d/%02d/%03d", date, month, year)) -- 日期格式化 Date formatting 02/01/2014
print(string.format("%.4f", 1 / 3)) -- 浮点数格式化 0.3333
array = { "hello", "world" }
for i = 0, 2 do
print(array[i])
end
-- result:
-- nil
-- hello
-- world
array = {}
for i = 1, 3 do
array[i] = {}
for j = 1, 3 do
array[i][j] = i * j
end
end
for i = 1, 3 do
for j = 1, 3 do
print(array[i][j])
end
end
--[[
1
2
3
2
4
6
3
6
9
]]
array = {}
for row = 1, 3 do
for col = 1, 3 do
array[row * 3 + col] = row * col
end
end
for row = 1, 3 do
for col = 1, 3 do
print(array[row * 3 + col])
end
end
--[[
1
2
3
2
4
6
3
6
9
]]
array = { "hello", "world" }
for key, value in ipairs(array) do
print(key, value)
end
--[[
1 hello
2 world
]]
function square(iteratorMaxCount, currentNumber)
if currentNumber < iteratorMaxCount then
currentNumber = currentNumber + 1
return currentNumber, currentNumber * currentNumber
end
end
for i, n in square, 3, 0 do
print(i, n)
end
--[[
1 1
2 4
3 9
]]
function square(iteratorMaxCount, currentNumber)
if currentNumber < iteratorMaxCount then
currentNumber = currentNumber + 1
return currentNumber, currentNumber * currentNumber
end
end
function squares(iteratorMaxCount)
return square, iteratorMaxCount, 0
end
for i, n in squares(3) do
print(i, n)
end
--[[
1 1
2 4
3 9
]]
function elementIterator(collection)
local index = 0
local count = #collection
-- 返回闭包函数
return function()
index = index + 1
if (index <= count) then
-- 返回迭代器当前元素
return collection[index]
end
end
end
array = { "hello", "world" }
for element in elementIterator(array) do
print(element)
end
--[[
hello
world
]]
local a = { [-1] = -1, 1, '2', nil, ['key'] = 'value', 6, [7] = 'end', ['kex'] = 'value2' }
for k, v in ipairs(a) do
print(k, v)
end
--[[
1 1
2 2
]]
for k, v in pairs(a) do
print(k, v)
end
--[[
1 1
2 2
4 6
7 end
key value
kex value2
-1 -1
]]
local a = { [1] = "hello", 2, [3] = 3, 4, 5 }
for k, v in ipairs(a) do
print(k, v)
end
--[[
结果:
1 2
2 4
3 5
]]
fruits = { "banana", "orange", "apple", "grapes" }
for k, v in ipairs(fruits) do
print(k, v)
end
table.sort(fruits)
print("sorted table")
for k, v in ipairs(fruits) do
print(k, v)
end
--[[
1 banana
2 orange
3 apple
4 grapes
sorted table
1 apple
2 banana
3 grapes
4 orange
]]
-- In file mymath.lua
local mymath = {}
function mymath.add(a, b)
return a + b
end
return mymath
-- In file main.lua
local mymath = require("mymath")
print(mymath.add(1, 2)) -- 3
mymetatable = {}
mytable = setmetatable({ key1 = "value1" }, { __newindex = mymetatable })
print(mytable.key1)
mytable.newkey = "new value 2"
print(mytable.newkey, mymetatable.newkey)
mytable.key1 = "new value 1"
print(mytable.key1, mymetatable.newkey1)
--[[
value1
nil new value 2
new value 1 nil
]]
-- rawset 不触发任何元方法将 mytable[key] 设置为 value
mytable = setmetatable({ key1 = "value1" }, {
__newindex = function (mytable, key, value)
rawset(mytable, key, "\""..value.."\"")
end
})
mytable.key1 = "new value"
mytable.key2 = 4
print(mytable.key1,mytable.key2)
--[[
new value "4"
]]
mytable = setmetatable({ 1, 2, 3 }, {
__add = function (mytable, newtable)
for i = 1, #mytable do
table.insert(mytable, #mytable + 1, newtable[i])
end
return mytable
end
})
secondtable = { 4, 5, 6 }
mytable = mytable + secondtable
for k, v in ipairs(mytable) do
print(k, v)
end
--[[
1 1
2 2
3 3
4 4
5 5
6 6
]]
mytable = setmetatable({ 10 }, {
__call = function (mytable, newtable)
local sum = 0
for i = 1, #mytable do
sum = sum + mytable[i]
end
for i = 1, #newtable do
sum = sum + newtable[i]
end
return sum
end
})
newtable = { 10, 20, 30 }
print(mytable(newtable)) -- 70
mytable = setmetatable({ 10, 20, 30 }, {
__tostring = function(mytable)
local str = "[ "
for k, v in pairs(mytable) do
str = str .. string.format("%d: %d, ", k, v)
end
str = string.sub(str, 1, #str - 2)
str = str .. " ]"
return str
end
})
print(mytable) -- [ 1: 10, 2: 20, 3: 30 ]