gpt4 book ai didi

lua - 有没有办法告诉 `next` 从特定键开始?

转载 作者:行者123 更新时间:2023-12-04 22:09:34 25 4
gpt4 key购买 nike

我的理解是 pairs(t) 只是简单地返回 next, t, nil

如果我将其更改为 next, t, someKey (其中 someKey 是我表中的有效键)将 next 开始于/在那个键之后?

我在 Lua Demo 上试过这个页面:

t = { foo = "foo", bar = "bar", goo = "goo" }

for k,v in next, t, t.bar do
print(k);
end

每次运行代码都会得到不同的结果。所以指定一个起始键是有效果的,不幸的是这个效果似乎有点随机。有什么建议吗?

最佳答案

每次运行遍历 Lua 表的程序时,顺序都会有所不同,因为 Lua 在内部使用哈希表中的随机盐。

这是在 Lua 5.2 中引入的。见 luai_makeseed .

关于lua - 有没有办法告诉 `next` 从特定键开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32279832/

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