Iterate through a list and process the strings....

Monkey Forums/Monkey Beginners/Iterate through a list and process the strings....

MonkeyPlotter(Posted 2016) [#1]
Hello, I've managed to parse some data from an xml type of file into four separate lists. I would like to iterate through one of the lists, which currently contains strings of 'float' type numbers. The strings are stored within a list called 'myList'. (its actually latOneList, but myList is a little more digestable!)

Can someone please provide an example of the syntax of how to iterate through myList? I intend to do a type conversion of each of the numerous strings which are generally in the format of 123.45678 into a float - and then store it in a float list 'myFloat'.

Then I'd like to iterate through the myFloat list and convert the floats into an arbitrary set of values between 0-255. I'm happy with the Maths bit concerning the conversion of my range of floats that can be processed - I've previously managed to implement a version of this code in Blitz 3D however I'm quite new to Monkey and this is my second hurdle I'm struggling to get over. This is how I'm parsing the xml data containing the float in question into a string containing only the 'float' numbers:-

If strSource[0 .. 17] = "<LatitudeDegrees>" Then
					latOneList = latOneList.Resize(latOneList.Length() + 1)
					strippedStr = strSource[17 .. 26]
					Print "LaTstrippedStr = "+strippedStr
					latOneList[latOneList.Length() - 1] = strippedStr
				End If

' ## example of line(s) being parsed:
' ## <LatitudeDegrees>53.0482628</LatitudeDegrees>
' ##





I appreciate there probably is a more succinct way to achieve what I want, but I'm in baby steps mode within Monkey at the mo, any help appreciated ;)


Goodlookinguy(Posted 2016) [#2]
Is this what you're asking for?

For Local item := EachIn myList
    ' do something
Next


If that is what you're asking for, you may want to take a look at the Monkey docs. Found here: http://www.monkey-x.com/docs/html/Programming_Language%20reference.html#statements

I should note that there is already an extremely robust and proper XML parser that was done by Skn3. This can be found here: http://www.monkey-x.com/Community/posts.php?topic=4125&page=first


degac(Posted 2016) [#3]
Hi,

I've managed to parse some data from an xml type of file into four separate lists.
I would like to iterate through one of the lists, which currently contains strings of 'float' type numbers.


In your esample it seems your are using Array[] to store informations

'pseudo code
p1=strSource.find("<LatitudeDegrees>)
p2=strSource.find("</LatitudeDegrees>)

if p1>-1 and p2>-1
strippedStr = strSource[p1..p2]
list_latidute.AddLast strippedStr
End If


And the use the code GoodLookinGui posted above to iterate in the list.

(I'm not on a computer with MonkeyX installed at the moment)

edited:
Example of a list (of Strings value)


By the way, supposing the data in XML are complete (longitude, latitude etc) it would be a better approach to create a Class (Position) to store these informations (with one single List, Map etc).


MonkeyPlotter(Posted 2016) [#4]
Thank you for the feedback, its really appreciated. Whilst I was tying myself in knots trying to suss the List syntax (I found an example of Lists within Lists - which I think was a step to far at the moment) - I thought about a class to hold the data, although it needs more thought, your idea about associating certain data to certain Map classes is an interesting one ;) :-

'  ## Potential class to shoehorn all the data into:-

'Class fourDatas
'					Field long:Int,lat:Int,bpm:Float,hrs:Int,mins:Int,secs:Int,dist:Int;x:Int,y:Int
'					Method New (long:Int,lat:Int,bpm:Float,hrs:Int,mins:Int,secs:Int,x:Int,y:Int)
'						Self.long = long
'						Self.lat = lat
'						Self.bpm = bpm
'						Self.hrs = hrs
'						Self.mins = mins
'						Self.secs = secs
'						Self.dist = dist
'						Self.x = x
'						Self.y = y
'					End Method
'End Class




Thanks for the tip about the xml parser as well - I was aware of it however I wanted to try and understand the syntax of Monkey before delving too deep. I'm off out this afternoon but relish trying this out later - for now - DIY, housework & stuff beckons. Cheers.


MonkeyPlotter(Posted 2016) [#5]
Thanks for the help, those Ints are so sweet ;) :--




Goodlookinguy(Posted 2016) [#6]
Did you fix that cast problem already? If not...
Local intVal:Int = Int(Float("53.048242"))



MonkeyPlotter(Posted 2016) [#7]
@Goodlookin, this was my method of casting, with a fudge to give me numbers to do some 'whole number' manipulation on:

'change the float into an int
					Local tempInt:Int = Int(tempFloat*1000000)
					
					'Print out the conversion debug
					Print "Float = " + tempFloat + " Int = " + tempInt




Thanks for the suggestion though - I'm kinda getting rid of all the decimilization prior to my version of the cast, unless of course I've implememented a bug that I haven't addressed yet - which you may well have provided a solution for.....


MonkeyPlotter(Posted 2016) [#8]
Mission successful, I have a square red rectangle moving within the browser smoothly around a course representative of the data imported from my GPS training watch - thanks for the assistance everyone, very much appreciated.