For key, item In Pairs(table) safe to remov in middle of ex?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

For key, item In Pairs(table) safe to remov in middle of ex?

Post by Bugala »

in post jul 09 at 11:08 am at: http://forums.hollywood-mal.com/viewtop ... =10&t=1659

I made this observer pattern thing.

In one part of the code it uses:

Code: Select all

Function observer:inform(message)
   For name, itemtable In Pairs(self.table)
      If itemtable.message = message Then observer:execute(name)
   Next
EndFunction
the idea is that it is going through every item on self.table, and if message is same as that table items is, then it executes the given function.
However, when that given function is being executed, it might remove that key, item pair from the list as a result of execution, and i started to wonder if this is safe thing to do?

To explain this easier, lets take another example which is giving the same problem:

Code: Select all

table = {"a", "a", "c", "c", "a"}
for n=0 to tableitems(table)-1
   if table[n] = "c" then removeitem(table, n)
next
What I might be wanting to do in this faulty example (it crashes at n=4, as that doesnt exist anymore at point of execution) is to check through every table if there are "c" items and then remove them out.

However, while the first "c" item is correctly removed, the next "c" will be left intact, because as "c":s are at indexes [2] and [3], when index[2] is removed, the next [3] = "c" becomes the new [2] = "c", and as executuion continues to next n, it is checking for [3], which is now the previous [4] = "a".


So what I am asking is that when using "for key, item in pairs(table)", is it safe to remove one item in middle of execution in same way as in that "for n=0 to tableitems" example? Or will i be encountering the same problem as in "for n=0 to tableitems" example?
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: For key, item In Pairs(table) safe to remov in middle of

Post by SamuraiCrow »

I would advise against it. A better approach would be to make a work list in another table and put the modifications there. This would be easier if order was not significant because you could save time from renumbering all the keys and just pull from a third list containing the unmodified pairs.
I'm on registered MorphOS using FlowStudio.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: For key, item In Pairs(table) safe to remov in middle of

Post by Bugala »

Yes, in the real version I am using (the observer list) it is based upon names.

the actual table works:

Code: Select all

table["name1"] = {}
table["name2"] = {}
Hence I am hoping that removing one in middle wouldnt affect the execution, but that is why I am asking the innerworks from Andreas to find out if i need to make different way to remove items (subtables).

Basically the thing is that suppose I have:

Code: Select all

table["a"] = {}
table["b"] = {}
table["c"] = {}
then if i use "for in pairs" and remove table["b"] in middle of execution, then does it still keep checking the table["c"], or does it actually work in way of each in pairs being [1], [2], [3], in which case removing [2], would mean that [3] would become new [2] and hence being skipped.

I hope i dont need to implement separate removal list feature since that can be problematic too. For most part it isnt much of a problem, but i can imagine some special situation when it could be a problem that i would then need to take extra care to avoid from happening.

I am also thinking of instead of making removal list, that maybe it would be safer to make my own iterator function to go through them. This way i could always ask what is the next item in list already in advance, and then when i remove the current item from list, i could still get to the next real item on list.
Post Reply