local numbers = {11, 2, 5, 7, 3}
-- unpack 把数组展开为参数;
-- 一百万个元素对它来说太多了
local min_value = math.min(table.unpack(numbers))
-- min_value = 2
local max_value = math.max(table.unpack(numbers))
-- max_value = 11
-- 最长的单词:保留到目前为止最大的
local words = {"bb", "a", "cccc"}
local longest = words[1]
for _, word in ipairs(words) do
if #word > #longest then
longest = word
end
end
-- longest = cccc
print("min_value = " .. min_value)
print("max_value = " .. max_value)
print("longest = " .. longest)
-- 空数组:没有参数,会报错
local empty = {}
local ok, err =
pcall(math.max, table.unpack(empty))
-- ok = false
-- err = bad argument #1 to 'math.max'
-- (value expected)
print("ok = " .. tostring(ok))
print("err = " .. err)