如何处理表格的插入和删除,并仍然知道哪个条目是哪个?

我刚开始接触Lua并遇到了一些问题。

我有一个表,其中包含应用程序中使用的对象实例。 我要对每个条目进行一些处理,并在完成后将其从表中删除。

假设情景是这样的:

  • 创建10个实例并将它们插入到表中

  • 对其中的一些进行计算[随机选择]

  • 计算完成后,从表中删除条目

  • 同时向表中添加10个实例

  • 在第6次计算后,从表中删除所有条目

由于我知道从一开始添加了多少对象,所以我将此计数用作条目的键:

table.insert(myTable, tostring(myObject.objectNumber), myObject)

我使用tostring来确保我不会遇到没有键问题[例如,计数从0开始]。

我想使用以下内容删除条目:

table.remove(myTable, tostring(myObject.objectNumber))

但是我必须将其作为第二个参数传递的不是键,而是表中的位置。这扰乱了整个想法,我有点迷失在如何正确删除条目而不必每次都在表格上循环中。我看不到任何可以通过键获得表位置的功能。

编辑: 因此,问题比我最初想象的要大一些。 首先:

table.insert(myTable, tostring(0), "something")
assert #myTable == 0

我可以在我的日志中看到:

enemy count:    0
Inserting   0   table: 0x18e8a50
Insert check    0   table: 0x18e8a50
enemy count:    0
Inserting   1   table: 0x18c7c40
Insert check    1   table: 0x18c7c40
enemy count:    1

这也不是由ipairs()返回的。

我不确定为什么,但事实就是如此。

其次,

Inserting   0   table: 0x1781ac60
Insert check    0   table: 0x1781ac60
Inserting   1   table: 0x5e8c20      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Insert check    1   table: 0x5e8c20 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Inserting   2   table: 0x17807390
Insert check    2   table: 0x17807390
Inserting   3   table: 0x5f5a30
Insert check    3   table: 0x5f5a30
Inserting   4   table: 0x18c7850
Insert check    4   table: 0x18c7850
Inserting   5   table: 0x5e15f0
Insert check    5   table: 0x5e15f0
Inserting   6   table: 0x1784c540
Insert check    6   table: 0x1784c540
Inserting   7   table: 0x5a7b80
Insert check    7   table: 0x5a7b80
Inserting   8   table: 0x18f6d30
Insert check    8   table: 0x18f6d30
Inserting   9   table: 0x189d3e0
Insert check    9   table: 0x189d3e0
remove  0   table: 0x1781ac60
remove check    0   nil 9
Inserting   10  table: 0x18e9c50
Insert check    10  table: 0x18e9c50
Inserting   11  table: 0x5d64a0
Insert check    11  table: 0x5d64a0
Inserting   12  table: 0x19d43540
Insert check    12  table: 0x19d43540
Inserting   13  table: 0x18d5730
Insert check    13  table: 0x18d5730
Inserting   14  table: 0x19d19110
Insert check    14  table: 0x19d19110
Inserting   15  table: 0x595800
Insert check    15  table: 0x595800
Inserting   16  table: 0x5e0f30
Insert check    16  table: 0x5e0f30
remove  5   table: 0x5e15f0
remove check    5   nil 16
remove  4   table: 0x18c7850
remove check    4   nil 16
remove  3   table: 0x5f5a30
remove check    3   nil 16
remove  2   table: 0x17807390
remove check    2   nil 16
remove  6   table: 0x1784c540
remove check    6   nil 16
remove  7   table: 0x5a7b80
remove check    7   nil 16
Inserting   17  table: 0x56fcf0
Insert check    17  table: 0x56fcf0
remove  1   table: 0x5e8c20 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
remove check    1   nil 17 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
remove  8   table: 0x18f6d30
remove check    8   nil 17
Inserting   18  table: 0x5970a0
Insert check    18  table: 0x5970a0
remove  9   table: 0x189d3e0
remove check    9   nil 18
Removing all entries:
1   table: 0x5e8c20     <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2   table: 0x17807390
3   table: 0x5f5a30
4   table: 0x18c7850
5   table: 0x5e15f0
6   table: 0x1784c540
7   table: 0x5a7b80
8   table: 0x18f6d30
9   table: 0x189d3e0
10  table: 0x18e9c50
11  table: 0x5d64a0
12  table: 0x19d43540
13  table: 0x18d5730
14  table: 0x19d19110
15  table: 0x595800
16  table: 0x5e0f30
17  table: 0x56fcf0
18  table: 0x5970a0
remove  1   table: 0x5e8c20 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

如您所见,虽然我之前已将条目删除,但似乎仍然存在。 现在..我认为这是表格的全局局部变量问题,但我已将条目计数添加到打印输出中[删除检查行的最后一个条目]。 “删除所有条目”后创建的列表是使用对(myTable)的循环创建的。

这是我插入和删除条目的代码:

MyObject = {}
MyObject_mt = { __index = MyObject }

function MyObject:new(params)
    MyObject = {
        objectNumber = params.objectNumber
    }
    local myObject = setmetatable(MyObject, MyObject_mt)

    print("Inserting", tostring(myObject.objectNumber), tostring(myObject))
    table.insert(myTable, tostring(myObject.objectNumber), myObject)
    print("Insert check", tostring(myObject.objectNumber), tostring(myObject))

    function MyObject:removeObject(event)
        print("remove", self.objectNumber, tostring(self))
        myTable[tostring(self.objectNumber)] = nil
        print("remove check", self.objectNumber, tostring(myTable[self.objectNumber]), #myTable)
        end
    end

    return myObject
end

原文链接 https://stackoverflow.com/questions/8452875

点赞
stackoverflow用户989510
stackoverflow用户989510

假设您没有连续的对象编号(即不要将表格用作数组,而是将其用作字典),则可以简单地执行以下操作:

table[tostring(myObject.objectNumber)] = nil;

就这样,条目被删除了。

2011-12-09 23:21:43