obtain the next list item value into a eachin loop

BlitzMax Forums/BlitzMax Beginners Area/obtain the next list item value into a eachin loop

hub(Posted 2011) [#1]

		For Local e:TScenario = EachIn Level_list
			If e.Level_Id = Level_Id Then
				'ok  i've founded the good e.Level id into the list, 
				' but now i want  access to the next list item value to have the Next e.Level_id value ?(if exist) and exit
				Exit
			End If
		Next

Thanks !

Last edited 2011


Jesse(Posted 2011) [#2]
untested but that is the logic
		Local link:TLink = level_list.firstLink()
		Local e:Tscenario
		While link 
			e = Tscenario(link.value())
			If e.Level_Id = Level_Id Then
				Local temp:TLink = link.nextLink()
				If temp 
					e = Tscenario(temp.value())		'ok  i've founded the good e.Level id into the list, 
				' but now i want  access to the next list item value to have the Next e.Level_id value ?(if exist) and exit
					Exit
				EndIf
			End If
			link = link.nextLink() 'get the next object in the list
			e = Null
		Wend


Last edited 2011

Last edited 2011


hub(Posted 2011) [#3]
Ok with Link. Thanks again Jesse.


Czar Flavius(Posted 2011) [#4]
Untested.
Type TScenario
	Global list:TList = New TList
	Global current_s:TLink
	
	Field id
	
	Function load_scenarios()
		'load the scenarios and add them to the list in the right order, or sort them after load
		If list.IsEmpty() Then RuntimeError "No scenarios loaded."
		restart()
	End Function
	
	Function restart()
		current_s = list.FirstLink()
		Assert current_s
	End Function
	
	Function get_current:TScenario()
		Local scen:TScenario = TScenario(current_s.Value())
		Assert scen
		Return scen
	End Function
	
	Function next_scenario:Int()
		'returns false and makes no change if no more scenarios
		Local link:TLink = current_s.NextLink()
		If link
			'we find a scenario
			current_s = link
			Return True
		Else
			'link is null, we're at the end of the list
			Return False
		End If
	End Function
	
	Function prev_scenario:Int()
		'returns false and makes no change if no more scenarios
		Local link:TLink = current_s.PrevLink()
		If link
			'we find a scenario
			current_s = link
			Return True
		Else
			'link is null, we're at the end of the list
			Return False
		End If
	End Function
	
	Function set_scenario:Int(id)
		'sets the current scenario to the one which matches this id number
		'returns false and makes no change if scenario not found
		'if you have more than a hundred scenarios, you should use a faster way
		
		'start at the beginning of the list
		Local link:TLink = list.FirstLink()
		
		'stop if we reach the end of the list
		While link
			'retreive the scenario at this location
			Local scen:TScenario = TScenario(link.Value())
			If scen.id = id
				'the id matches
				current_s = link
				Return True
			Else
				'the id doesn't match, next link
				link = link.NextLink()
			End If
		Wend
		'if we get here, then we did not find a matching scenario
		Return False
	End Function
End Type


Last edited 2011


hub(Posted 2011) [#5]
Many thanks too.