## Removing a table from another table

General discussion about LÖVE, Lua, game development, puns, and unicorns.
yoyohoho
Prole
Posts: 14
Joined: Tue Apr 14, 2020 12:07 pm

### Removing a table from another table

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})


zorg
Party member
Posts: 2913
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Removing a table from another table

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.
Last edited by zorg on Fri May 22, 2020 7:35 am, edited 2 times in total.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

inJuly
Prole
Posts: 20
Joined: Fri May 01, 2020 9:02 pm

### Re: Removing a table from another table

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.

pgimeno
Party member
Posts: 2170
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Removing a table from another table

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

zorg
Party member
Posts: 2913
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Removing a table from another table

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...
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

pgimeno
Party member
Posts: 2170
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Removing a table from another table

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 am
it 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.

### Who is online

Users browsing this forum: Google [Bot] and 23 guests