How does ForEach react if more items are added?

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

How does ForEach react if more items are added?

Post by Bugala »

Instead of trying out, I decided to ask in case there are some exceptions to this rule, especially since one already comes to my mind.

Suppose I am making a game with locations, and some sort of pathfinding might become one way or other necessary.

what comes to my mind first is that each location would have subtable called "neighbors".

as example:

Code: Select all

locations = { 
           ["Finland"] = [ neighbors = { "sweden", "norway", "russia", "estonia"} },
           ["Sweden"] = [ neighbors = { "Finland", "Norway", "denmark"} },
           ...
              }
what could happen during the program is, that i make some temporary table, like "locationstocheck = {}" Where i would use InsertItem to add more country names.

Idea would be that each name in that list, would do so that they would check through all their neighbors and in some certain cases, neighbors could be added to the locationstocheck list:

Code: Select all

foreach(t_locationstocheck, Function (cell, data)
                                              foreach(cell.neighbors, Function (index, value)
                                                                       if index.friendly=true then insertitem(t_locationstocheck, value)
                                                                                 EndFunction)
                                          EndFunction)
as you can see, the idea is that when the ForEach command goes through "t_locationstocheck" table, that table might grow during the process.

Hence how does Hollywood work with this?
1. It only executes table in its original form.
2. It also executes everything added to the end of the list during the process.

And if number 2, then how does index 0 work out? Since if i understood right, index 0 is executed last, but what if that index 0 has some neighbors that are added to the t_locationstocheck, does it then happen, that these ones are not executed at all, or will these new ones be executed as well, and if they are as well executed, then will index 0 be executed again?


And as last question regarding this; Are there some other things that I should take into consideration if I am planning to use this method?
User avatar
airsoftsoftwair
Posts: 5446
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: How does ForEach react if more items are added?

Post by airsoftsoftwair »

This is illegal. It's only allowed to modify existing table elements while in a ForEach() loop. The behaviour is undefined if you add or remove table elements.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: How does ForEach react if more items are added?

Post by Bugala »

Okay, i guess i better find another solution then.

Just to be more clear, did i understand that the point is that if it happens to work this way, it then works, but that because it is undefined, there is no guarantee it will work right on next hollywood version anymore?

That the behavior isnt random each time something like this is done, just that there is no guarantee the behavior stays the same from one version to next one.
User avatar
airsoftsoftwair
Posts: 5446
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: How does ForEach react if more items are added?

Post by airsoftsoftwair »

It might not even work with the same version. Undefined means undefined, you can be lucky or unlucky, everything could happen, don't count on anything, so better keep your hands off expecting anything from undefined behaviour :)
Post Reply