Page 1 of 1

lua table parsing and remove item

Posted: Fri Jul 22, 2016 11:57 am
by binbinhfr
Hi,

as many mods around, to delete some unwanted items from a list, I often use table.remove inside an iterative pairs-loop.
ex :

Code: Select all

for i, entity in pairs(entities) do
	if not(entity and entity.valid) then
		table.remove(entities,i)
	end
end
But reading this, I have some doubt that some items can be skipped :
http://stackoverflow.com/questions/1239 ... -iterating

Any idea or precision on this ? Why modders do not seem to care about this problem ?

Re: lua table parsing and remove item

Posted: Fri Jul 22, 2016 2:27 pm
by DedlySpyder
Hm, I didn't even know that existed. When I made my first mod using tables I looked at rail tanker fir an example, and in that elements are removed from the table by creating a new table without the one to be removed and overwrites it. Not sure which way is better, that just ended up being the thing I used since

Re: lua table parsing and remove item

Posted: Fri Jul 22, 2016 3:49 pm
by binbinhfr
> Not sure which way is better

your way is safe, but on a large table, I suppose that it is not optimal.

Re: lua table parsing and remove item

Posted: Fri Jul 22, 2016 4:10 pm
by Klonan
Generally for the use of a factorio mod, skipping a couple of entities each iteration loop is an inconsequential error

Re: lua table parsing and remove item

Posted: Fri Jul 22, 2016 4:50 pm
by binbinhfr
Klonan wrote:Generally for the use of a factorio mod, skipping a couple of entities each iteration loop is an inconsequential error
Well not always. ;) I had a major factorio crash because doing it this way while cleaning unvalid entities in on-config-changed event after a mod uninstall.
Now I do it by table recopy and there is no more crash-log.

Re: lua table parsing and remove item

Posted: Wed Jul 27, 2016 8:18 am
by bobingabout
For removing entities, iterating backwards is preferable to iterating forwards. I've just not seen a method of doing that in LUA yet. (well, you can create that for loop manually, but can't use it with pairs)

Re: lua table parsing and remove item

Posted: Wed Jul 27, 2016 8:35 am
by binbinhfr
bobingabout wrote:For removing entities, iterating backwards is preferable to iterating forwards. I've just not seen a method of doing that in LUA yet. (well, you can create that for loop manually, but can't use it with pairs)
Yes, I just wonder with all these indexed access, what is the less cpu demanding method, but still safe... Recreating a table or parsing it and removing objects ? It must demand on the average percentage of objects you have to remove at each call...

Re: lua table parsing and remove item

Posted: Wed Jul 27, 2016 9:22 pm
by Supercheese
bobingabout wrote:For removing entities, iterating backwards is preferable to iterating forwards. I've just not seen a method of doing that in LUA yet. (well, you can create that for loop manually, but can't use it with pairs)
Yeah, I do this (the manual way) in one of my mods: https://github.com/Suprcheese/EMP-Biter ... ua#L48-L67

Re: lua table parsing and remove item

Posted: Thu Jul 28, 2016 10:47 am
by Adil
bobingabout wrote:(well, you can create that for loop manually, but can't use it with pairs)
Why would you need pairs if you have a manual for loop?

Re: lua table parsing and remove item

Posted: Thu Jul 28, 2016 11:37 am
by binbinhfr
Adil wrote:Why would you need pairs if you have a manual for loop?
I often read that parsing pairs is much quicker and optimised in LUA that using a repeteated index to access every object of the table. No ?

Re: lua table parsing and remove item

Posted: Fri Jul 29, 2016 6:07 pm
by Adil
binbinhfr wrote: I often read that parsing pairs is much quicker and optimised in LUA that using a repeteated index to access every object of the table. No ?
That might be applicable to pairs() vs ipairs(). (But actually conflicting results can be googled on that)
But I really fail to see how that could be for numerical loop. That one infers only a single value lookup and incrementation of value, whereas ipairs() has however small but unneded function call overhead, and pairs() has to maintain the list of elements already seen somewhere internally.
https://springrts.com/wiki/Lua_Performa ... _for-loops