【发布时间】:2014-01-13 13:15:07
【问题描述】:
假设我有这本 Lua 字典
places = {dest1 = 10, dest2 = 20, dest3 = 30}
在我的程序中,我检查字典在这种情况下是否达到了我的大小限制 3,如何将最旧的键/值对推出字典并添加一个新的?
places["newdest"] = 50
--places should now look like this, dest3 pushed off and newdest added and dictionary has kept its size
places = {newdest = 50, dest1 = 10, dest2 = 20}
【问题讨论】:
-
你为什么要这个?
-
字典键不按你输入的顺序保存,不像索引,所以我不知道你能不能把它推到前面。你/可以/用一个索引表来做(在意识到你想要字典之前我写了一个函数)。
-
@lhf 仅用于编写游戏脚本,我需要一个具有固定大小的字典,并在添加新对时推出最旧的键/值对(一旦达到固定大小)。
标签: dictionary lua lua-table