local dic = {one = "A", two = "B"}
-- nil is a missing key: or gives the default
local value = dic.three or "-"
-- value = -
-- Store the default as well
dic.three = dic.three or "-"
-- dic.three = -
-- Counting: a missing key starts from 0
local scores = {}
scores.ann = (scores.ann or 0) + 1
-- scores.ann = 1
-- Trap: or replaces a stored false, too
local flags = {sound = false}
local sound1 = flags.sound or true
-- sound1 = true
-- Test for nil to keep the false
local sound2 = flags.sound
if sound2 == nil then
sound2 = true
end
-- sound2 = false
-- A default for every missing key at once
local counters = setmetatable({}, {
__index = function()
return 0
end,
})
counters.ann = counters.ann + 1
-- counters.ann = 1
local bob = counters.bob
-- bob = 0
print("value = " .. value)
print("dic.three = " .. dic.three)
print("scores.ann = " .. scores.ann)
print("sound1 = " .. tostring(sound1))
print("sound2 = " .. tostring(sound2))
print("counters.ann = " .. counters.ann)
print("bob = " .. bob)