gpt4 book ai didi

lua - 字符串中的 Redis-Lua 数组键

转载 作者:可可西里 更新时间:2023-11-01 11:13:10 25 4
gpt4 key购买 nike

我在Redis下执行Lua。我面临一个问题,即我不能将字符串用作数组键。我的编码如下,我们发现 mytable["wow"] 被丢弃了:

文件名:hget.lua

local mytable = {}
mytable[1]= "Lua"
mytable["wow"] = "Tutorial"
return mytable

Command:redis-cli --eval hget.lua

返回的结果是:

1) "Lua"

最佳答案

如果要将表返回到 Redis,则表不能有字符串键。

Redis 将返回的表作为数组,索引从1 开始。它丢弃表中键不是整数的其他元素。在你的例子中,即 mytable["wow"] = "Tutorial",因为键是一个字符串,Redis 忽略了这个元素。

此外,索引必须是顺序的,否则,Redis 会丢弃一些元素。以下面为例:

local t = {}
t[1] = "1" -- OK
t[2] = "2" -- OK
t[4] = "4" -- since index 3 is missing, this element will be discarded
t["string_key"] = "value" -- since the key is string, this element will be discarded

return t

结果:

./redis-cli --eval t.lua 
1) "1"
2) "2"

关于lua - 字符串中的 Redis-Lua 数组键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40167873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com