Is there a way to send own variables with foreach command?

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

Is there a way to send own variables with foreach command?

Post by Bugala »

What I am trying to do is following:

(this code is not working)

Code: Select all


function p_OwnLoop(key, value, local sublevel)
  if sublevel < 5 
     sublevel=sublevel+1
     foreach (table, p_OwnLoop(sublevel)
  else
     do something
  endif

endfunction

What I am in pracitce trying to do, is to go through every shoe stored in table. However, Shoes are in way of "Gabor 51.320.27 Size 5", which makes this multidimensional table ([Brand][numbers1][numbers2][numbers3][size])

Since I can only do the thing i want when i reach the size point, I am trying to figure out a way to go through every item, and only do the thing after the size point is reached (which is reason for if sublevel < 5)

For this reason I would want to keep looping through same function using foreach command, but i somehow need to know in which sublevel (brand, numbers 1 - 3, size) I am going, so when i reach the Size point, Function knows to do something else.


My first idea was to use global variable sublevel, but then i realised it would keep increasing even beyond 5. So my current idea would be to use Local sublevel variable which I think would solve the problem, but question is, how can i (or can i) pass the variable sublevel to next foreach loop and have it as local variable.
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Re: Is there a way to send own variables with foreach command?

Post by Bugala »

Heres the code i currently have in my project:

Code: Select all


..
ForEach(storage, p_DrawAllPackages)
..

Function p_DrawAllPackages(key, info)
	ForEach(Info, p_numbers1)
EndFunction

Function p_numbers1(key, info)
	ForEach(Info, p_numbers2)
EndFunction

Function p_numbers2(key, info)
	ForEach(Info, p_colors)
EndFunction

Function p_colors(key, info)
	If GetType(info) = #TABLE Then ForEach(Info, p_sizes)
EndFunction

Function p_sizes(key, info)
	If GetType(info) = #TABLE Then ForEach(info, p_IndividualPackages)
EndFunction

Function p_IndividualPackages(key, info)
	If GetType(info) = #TABLE Then p_DrawPackage(info)
EndFunction

It would be very nice if I could replace these all with just one function into which i would keep jumping to again and again, but to do this, i would need some way to know in which level of the multidimension table (storage[brand][numbers1][numbers2][color][size][shoes][individualshoe]) the program is going at, which could simply be solved if there would be some way to send some variable with foreach command.

But is there any way to make this any better than this?
Post Reply