TList help

BlitzMax Forums/BlitzMax Programming/TList help

Curtastic(Posted 2005) [#1]
this is supposed to loop forever because it adds a new element to the list every iteration.
It works only if the list starts with at least 2 elements.

Its like if I try to make a flood-fill, it will start with 1 element, then loop, and each element will create more elements until the area is full.
What should I do, and is this a bug?
Type rat
EndType

Local r:rat
list:TList=New TList

ListAddLast list,New rat

ListAddLast list,New rat 'remove this line and it doesn't work

Print "Rats forever breeding!"
For r=EachIn list
	add:+1
	Print add

	ListAddLast list,New rat

	Delay 500
Next

Print "DONE? rats are not asexual"



tonyg(Posted 2005) [#2]
My theory (i.e. could be wrong) is 'For/Eachin' checks how many instances there are each loop and decides that's how many times to run.
In the '1 instance' case it decides to run once at the top of the loop so, even though there's a listaddlast, that's what it does... runs one loop and finishes.
For the '2instance' example, it decides to run twice. When it gets to the top of the second loop it runs the same check and decides to loop twice etc etc.
If that's true, I'd say it was a bug and BMax should check whether the count is as expected at the end of the loop.


Augen(Posted 2005) [#3]
Hmm also, when you COMMENT or UNCOMMENT the line,
ListAddLast list,New rat

within the loop, AND UNCOMMENT 1 of the lines
ListAddLast list,New rat

outside the loop, it gives the same result of ending with a display of
Rats forever breeding!
1
DONE? rats are not asexual

Process complete

edit
However, if both the lines outside the loop are uncommented and the line within the loop is commented, the result is
1
2
DONE? rats are not asexual

Process complete


which is expected.


Tibit(Posted 2005) [#4]
Are you using a For Eachin as your main loop?

The For Eachin loop loops one time for every object you have in that TList. In you case you ask it too loop one or zero times - it will never re-evaluate the list length because it never actually loops.

But there is no actuall reason it shouldn't do that check when it reach the next when we got one element. Perhaps that is unintentional?

To solve your problem use another loop to loop your rats.




Curtastic(Posted 2005) [#5]
okay thanks, I just put a repeat until around my For loop.

My blitz3d code is like this:
Type spot
	Field x,y
End Type

Function fill()
	s.spot=New spot
	s\x=startx
	s\y=starty
	
	For s=Each spot
		For angle=1 To 4
			newx=s\x+Cos(angle*90)
			newx=s\y+Sin(angle*90)
			If map(newx,newy)=0 Then
				s2.spot=New spot
				s2\x=newx
				s2\y=newy
				map(newx,newy)=1
			EndIf
		Next
		Delete s
	Next
End Function

It doesn't work after converting to blitzmax so I added this:

repeat
	For s=Eachin spotlist
		...
	next
until listisempty(spotlist)