S
local set = {
a = true,
b = true,
foo = true,
["foo"] = true -- то же самое
}
-- это всё - одно и то же
if set.foo then ... end
if set["foo"] then ... end
local lactac = "foo"
-- по значению переменной
if set[lactac] then ... end
Size: a a a
S
local set = {
a = true,
b = true,
foo = true,
["foo"] = true -- то же самое
}
-- это всё - одно и то же
if set.foo then ... end
if set["foo"] then ... end
local lactac = "foo"
-- по значению переменной
if set[lactac] then ... end
S
for i = 1, #set do
if b == set[i] then return true end
end
S
set = {a,b,c,d,e}
это чистый незамутнённый массив со значениями переменных a
, b
, c
, d
и e
, а не хеш-мапа.F
for i = 1, #set do
if b == set[i] then return true end
end
S
S
function find_in_array(arr, v, offset)
for i = offset or 1, #arr do
if arr[i] == v then return i end
end
return nil
end
S
b = 10
set = {a, b, c, "d", e}
print( set[1] ) --> nil
print( set[2] ) --> 10
print( set[3] ) --> nil
print( set[4] ) --> d
print( set["a"] ) --> nil
print( set.b ) --> nil
Это массив. Тут только числовые ключи.set = {[1] = a, [2] = b, [3] = c, [4] = "d", [5] = e}
S
for i, item in ipairs(array) do
, что есть примерно то же но лучше (ибо встроенный счётчик).S
b = 10
set = {a = 1, b = 2, ["c"] = 3, "d", [b] = 4, "e"}
^ хеш ^ хеш ^ хеш ^массив ^ хеш ^ массив
-- "e" = "bar" -- ошибка синтаксиса, нужно точно определить что "e" это ключ
print( set[1] ) --> "d" -- потому что оно попало в массивную часть
print( set[2] ) --> "e"
print( set.a ) --> 1
print( set.b ) --> 2
print( set.c ) --> 3
print( set['c'] ) --> 3 -- то же самое
print( set[b] ) --> 4 -- [b] это [10]
print( set[10] ) --> 4
S
if set[item] then
S
S
function Set(list)
local set = {}
for _, l in ipairs(list) do set[l] = true end
return set
end
S
S
function table_invert(src)
local dst = {}
for key, value in pairs(src) do
dst[value] = key
end
return dst
end
Типа, оно может превратить{ foo = 1, bar = 2 }в
{"foo", "bar"}
и обратноF