字典初始化

数组和集合 / 字典

-- Keys that are names need no quotes
local d1 = {one = 1, two = 2}
-- d1.one = 1
-- d1.two = 2

-- Any other key goes in square brackets
local d2 = {["two words"] = 2, [10] = "ten"}
-- d2["two words"] = 2
-- d2[10] = ten

-- Empty table, filled from two arrays
local names = {"one", "two", "three"}
local numbers = {1, 2, 3}
local d3 = {}
for index, name in ipairs(names) do
  d3[name] = numbers[index]
end
-- d3.three = 3

print("d1.one = " .. d1.one)
print("d1.two = " .. d1.two)
print('d2["two words"] = ' .. d2["two words"])
print("d2[10] = " .. d2[10])
print("d3.three = " .. d3.three)