Page 1 of 1
Removing a table from another table
Posted: Fri May 22, 2020 7:15 am
by yoyohoho
Hello out there
I have a problem when I'm trying to remove a table from another table.
Code: Select all
objects = {}
local lock = GameObject {
x = number
y = number
texture = 'string'
otherstuff = true
table.insert(objects, lock)
Now I want to remove this object again. However, when I try to remove with
pair it seems to delete the other objects in the
objects table, seemingly randomly. How do I access
lock in the
objects table?
The way I've inserted other objects into this table is by saying:
Code: Select all
table.insert(objects,
GameObject {
x = number
y = number
texture = 'string'
otherstuff = true})
Re: Removing a table from another table
Posted: Fri May 22, 2020 7:33 am
by zorg
table.insert by default inserts at the end of the array part of the table.
the array part is whichever n,n+1,...,n+k continuous sequence of positive whole numbers that lua decides is the array part.
if you only ever used table.insert to insert stuff into your objects table, then you can simply iterate over it using ipairs instead of pairs, since the latter does things in an undefined order.
i would ask though, how do you differentiate between objects in what you want to remove? by which field? because putting a table in a local variable called lock does nothing when you insert it into objects; the table itself won't store that it was the 'lock' table, you'd need to store that separately... that said, without these answers, i can only guess and give general advice.
Also, as the ninja below me said, if you don't want holes in your table, using table.remove is an easy solution.
Re: Removing a table from another table
Posted: Fri May 22, 2020 7:33 am
by inJuly
If you're using a table as an array/ list then you can simply keep track of the index and call table.remove(objects, index), and it will remove the element at that index and adjust the indices of other elements accordingly. Or, you could do this safely by traversing the table from last to first (this prevents from deleting other elements accidentally)
You could also manually traverse the array if you don't want to keep track of what index it's at
Code: Select all
for i = #objects, 1, -1 do
if objects[i] == lock then
table.remove(objects, i)
break
end
end
This way the indices are readjusted.
If you don't traverse the table anywhere else in the codebase then you're simply using it as a map then
just replace table.remove(objects, i) with objects
= nil.
Re: Removing a table from another table
Posted: Fri May 22, 2020 8:06 am
by pgimeno
zorg wrote: ↑Fri May 22, 2020 7:33 am
the array part is whichever n,n+1,...,n+k continuous sequence of positive whole numbers that lua decides is the array part.
Only for n = 1
Re: Removing a table from another table
Posted: Fri May 22, 2020 11:13 am
by zorg
pgimeno wrote: ↑Fri May 22, 2020 8:06 am
zorg wrote: ↑Fri May 22, 2020 7:33 am
the array part is whichever n,n+1,...,n+k continuous sequence of positive whole numbers that lua decides is the array part.
Only for n = 1
Maybe true for the range ipairs iterates over, but e.g. the # operator isn't guaranteed to give back e.g. 3 in a case like this:
Code: Select all
local a = { [1] = true, [2] = true, [3] = true, -- 4 skipped
[5] = true, [6] = true, [7] = true, [8] = true, [9] = true, [10] = true, [11] = true, [12] = true}
print(#a)
it could return 8... at least i think i remember something like this... unless i'm mixing it up with some other unscrupulous feature...
Re: Removing a table from another table
Posted: Fri May 22, 2020 3:06 pm
by pgimeno
Yeah and it is probably treated as an array internally, but I think we need an operative definition, not an implementation-based definition. Relying on the underlying implementation is a disaster waiting to happen. (Edit: Also, Lua 5.2 calls them sequences, and defines them as "a table where the set of all positive numeric keys is equal to {1..n} for some integer n, which is called the length of the sequence". That's a good operative definition.)
zorg wrote: ↑Fri May 22, 2020 11:13 amit could return 8... at least i think i remember something like this... unless i'm mixing it up with some other unscrupulous feature...
Not really. "The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil", says the manual for 5.1, so in your example, it's either 3 or 12.