在Lua中映射多值元组的多键元组
Lua中是否有支持元组到元组映射的库?我有一个键{a,b,c}映射到一个值{c,d,e}在Lua中映射多值元组的多键元组,lua,Lua,Lua中是否有支持元组到元组映射的库?我有一个键{a,b,c}映射到一个值{c,d,e} 有LIB,例如,对于多键,但不是值为元组的LIB。这里有一种方法可以使用Egor的建议,通过字符串连接生成键。为表t创建自己的简单insert和get方法 local a, b, c = 10, 20, 30 local d, e, f = 100, 200, 300 local t = {} t.key = function (k) local key = "" for _,v in ipair
有LIB,例如,对于多键,但不是值为元组的LIB。这里有一种方法可以使用Egor的建议,通过字符串连接生成键。为表t创建自己的简单insert和get方法
local a, b, c = 10, 20, 30
local d, e, f = 100, 200, 300
local t = {}
t.key = function (k)
local key = ""
for _,v in ipairs(k) do
key = key .. tostring(v) .. ";"
end
return key
end
t.set = function (k, v)
local key = t.key(k)
t[key] = v
end
t.get = function (k)
local key = t.key(k)
return t[key]
end
t.set ({a, b, c}, {d, e, f}) -- using variables
t.set ({40, 50, 60}, {400, 500, 600}) -- using constants
local w = t.get ({a, b, c}) -- using variables
local x = t.get ({40, 50, 60}) -- using constants
print(w[1], w[2], w[3]) -- 100 200 300
print(x[1], x[2], x[3]) -- 400 500 600
将元组转换为字符串
tostring(a);“.tostring(b);”.tostring(c)
,并将此字符串用作索引。我看不到链接库中值的类型有任何限制。您的“插入”与table.insert有很大不同。请用“set”作为“get”的补语。