lua table parsing and remove item

Place to post guides, observations, things related to modding that are not mods themselves.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

lua table parsing and remove item

Post 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 ?
My mods on the Factorio Mod Portal :geek:
User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 254
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: lua table parsing and remove item

Post 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
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: lua table parsing and remove item

Post by binbinhfr »

> Not sure which way is better

your way is safe, but on a large table, I suppose that it is not optimal.
My mods on the Factorio Mod Portal :geek:
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5341
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: lua table parsing and remove item

Post by Klonan »

Generally for the use of a factorio mod, skipping a couple of entities each iteration loop is an inconsequential error
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: lua table parsing and remove item

Post 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.
My mods on the Factorio Mod Portal :geek:
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: lua table parsing and remove item

Post 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)
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: lua table parsing and remove item

Post 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...
My mods on the Factorio Mod Portal :geek:
Supercheese
Filter Inserter
Filter Inserter
Posts: 841
Joined: Mon Sep 14, 2015 7:40 am
Contact:

Re: lua table parsing and remove item

Post 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
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: lua table parsing and remove item

Post 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?
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: lua table parsing and remove item

Post 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 ?
My mods on the Factorio Mod Portal :geek:
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: lua table parsing and remove item

Post 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
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
Post Reply

Return to “Modding discussion”