查找元素

数组和集合 / 集合(Set)

local chars = {A = true, B = true, C = true,
               D = true}
local contain_a = chars.A
-- contain_a = true

-- A missing item gives nil, not false
local contain_e = chars.E
-- contain_e = nil

-- A real boolean: compare with true
local has_e = chars.E == true
-- has_e = false

-- Subset check: every item of chars2 is found
local chars2 = {A = true, B = true}
local contain_all = true
for char in pairs(chars2) do
  if not chars[char] then
    contain_all = false
    break
  end
end
-- contain_all = true

print("contain_a = " .. tostring(contain_a))
print("contain_e = " .. tostring(contain_e))
print("has_e = " .. tostring(has_e))
print("contain_all = " .. tostring(contain_all))