go through list backwards

Monkey Forums/Monkey Programming/go through list backwards

slenkar(Posted 2011) [#1]
is it possible to go through a list backwards?


Canardian(Posted 2011) [#2]
You could use a Stack and access the elements with the Get(index) method.


slenkar(Posted 2011) [#3]
thanks for the suggestion , ill take a look


Wagenheimer(Posted 2011) [#4]
What is a stack?

I need to get a random item in a list, is that possible? I could not find the .get method in the TList.


therevills(Posted 2011) [#5]
You could do something like this:

	Function getRandomSprite:Sprite()
		Local r% = 0
		Local rand% = Rnd(0, Sprite.list.Count())
		Local rndSprite:Sprite
		For Local s:Sprite = Eachin Sprite.list
			If r = rand Then
				rndSprite = s
				Exit
			EndIf
			r+=1
		Next
		Return rndSprite
	End Function


Full Code:



Jesse(Posted 2011) [#6]
you can use nodes to remove items from the list such as with with Bmax's "Tlink".


Canardian(Posted 2011) [#7]
@Wagenheimer: a Stack is like a List, but it has direct .Get/.Set methods. See in modules/monkey/stack.monkey for documentation about the commands.


slenkar(Posted 2011) [#8]
i changed my list to a stack and it was easy to go backwards through it