Lua 表 - 当删除较低的索引时,较高的索引全部下移 1 位以填补该位置
使用使用的 Lua 是 5.1
我试图获取一个字典表并将其存储在一个表中,其中我有一个键索引,值是数据,类似于:
tParams = {
{1st table data...} -- this one is key index 1
{2nd table data...} -- 2nd index
...
}
我正在使用此代码来存储它:
tParams[index] = i --i being a dictionary table
我可以添加正确索引中的代码,如果调用则输出表,所以一切都很好
1 table: 0xa4a2fc0
2 table: 0x9f7c4c0
现在问题是,如果我删除例如第一个索引数据,我不希望第二个索引数据移动到第一个位置只是因为它是空的只是取代它并成为索引 1...我怎样才能避免这种情况?
或者,如果您通常有另一个解决方案,我想做的是存储我正在获取的字典表数据,如果被调用,则能够获取数据并使用它,如果我不再需要它们来删除它们。
可能有很多多个字典,具体取决于创建的实例数量,所以我不能只是预先制作它们。 它们基本上是动态的……每次它们都有一个特定的 id 时,它们就会随之而来,所以 2 个字典不会有相同的 id。
如果删除字典,其他字典不应移动并保留在相同的索引/id 中,以便可以调用它们并成为正确的数据。
我希望我已经说清楚了,也请不要建议任何补充模块。
谢谢!
Using Lua used is 5.1
I'm trying to get a dictionary table and store it in a table where i have a key index and the value being the data, something like:
tParams = {
{1st table data...} -- this one is key index 1
{2nd table data...} -- 2nd index
...
}
I'm using this code to store it:
tParams[index] = i --i being a dictionary table
I can add the code in the right index and the table is output if called, so all fine
1 table: 0xa4a2fc0
2 table: 0x9f7c4c0
Now the problem is if I remove for example the first index data, i DON'T want the 2nd index data to move to the 1st position just because it's empty it just takes it place and becomes index 1... how can I avoid that?
Or if you have another solution in general, what i wanna do is store dictionary table data i'm getting, and if called, to be able to get the data and work with it and if i don't need them anymore to remove them.
There can be many multiple dictionaries depending on how many of the instances are created so i can't just pre make them.
They will be dynamic basically... and every time they have a specific id they come with so 2 dictionaries will not have the same id.
If a dictionary is removed, the other dictionaries should not move and stay in the same index/id so they can be called and be the right data.
I hope i was clear, also please don't suggest any supplementary modules.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提供所有相关代码,但从表中删除项目的基本方法是
tParams[index] = nil
。这不应该导致您所描述的问题。听起来好像您正在使用
table.remove
。该函数用于维护正确的顺序,以便#
运算符 和一些标准库函数对其来说是有意义的。You're not providing all the relevant code, but the basic way to remove an item from a table is
tParams[index] = nil
. That should not cause the problem you're describing.It sounds as if you're using
table.remove
. That function is used for maintaining a proper sequence, so that the#
operator and some standard library functions can be meaningful for it.