For/each noob question

Monkey Forums/Monkey Programming/For/each noob question

pinete(Posted 2011) [#1]
Hi, I have a really noob question..
Could you please help me to traslate this simple piece of code to Monkey? It's needed to use lists?
Thanks a lot, your help will help me to have a clear idea about some basic stuff. :)
Thanks a lot!


Type TExample
Field x,y
Field ref

for t.TExample = each TExample
if t.x = 10 then xxxxx
if t.y > 5
t.ref = t.ref + 1
endif
next


Canardian(Posted 2011) [#2]
I prefer Stack instead over List, because Stack has a Get(index) method, so I can access elements by an index value:
Class Example
	Field x,y
	Field ref
End

Function Main()
	Local examples:=New Stack<Example>

	Local temp:Example=New Example
	temp.x=1
	temp.y=2
	temp.ref=3
	examples.Push temp

	temp=New Example
	temp.y=6
	examples.Push temp

	temp=New Example
	temp.ref=8
	examples.Push temp

	For Local t:Example=Eachin examples
		Print t.x+","+t.y+","+t.ref
	Next
End



DGuy(Posted 2011) [#3]
Class CExample
    Field x, y
    Field ref
End

...

[Global|Local|Field] MyList:List<CExample> = New List<CExample>

...

MyList.AddLast( ... )

...

For Local t:=EachIn MyList
    If t.x = 10 Then ...
    If t.y > 5 Then t.ref += 1
End



pinete(Posted 2011) [#4]
Thanks so much Lumooja and DGuy, much appreciated ;) your examples has helped me a lot understanding how the things are done.
Great!
you both rock! :)