I just began my adventure with Lua and I’m hitting some issues.
I’ve got a table, which holds instances of object that are use in my application.
I do some stuff with every entry and when I’m done I want to remove it from the table.
Let’s say the scenario is like this:
-
create 10 instances and insert them into the table
-
do some calculations on some of them [random choice]
-
once calculation is done, remove entry from the table
-
in the meantime add 10 more instances to the table
-
after 6th calculation remove all entries from the table
Since I know how many objects were added from the beginning I am using this count as key of the entry:
table.insert(myTable, tostring(myObject.objectNumber), myObject)
I am using tostring to make sure I don’t run into no key issues [for example, count starts with 0].
I wanted to remove entries using:
table.remove(myTable, tostring(myObject.objectNumber))
But it’s not the key that I have to pass as the second argument but position in the table. This screws the whole idea and I’m a bit lost on how to remove the entry properly without doing a loop over table each time. I cannot see any function which would give me table position of an entry by key.
EDIT:
So the problem is a little bit larger then I thought initially.
First of all:
table.insert(myTable, tostring(0), "something")
assert #myTable == 0
I can see that in my logs:
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
Also it is not returned by ipairs().
I’m not sure why, but it is how it is.
Second of all,
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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
As you can see although I have removed the entry earlier, it seems to still be there.
Now.. I thought it was a global-local variable issue with the table, but I’ve added the count of entries to the print [last entry in remove check lines].
The list after “Removing all entries” was created using for loop over pairs(myTable).
Here’s the code where I insert and remove entries:
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
Well assuming you dont have consecutive object numbers (i.e. don’t use the table as an array but rather as a dictionary) you can do simply a
Thats it, entry removed.