for loops and lists?
BlitzMax Forums/BlitzMax Beginners Area/for loops and lists?
| ||
Ok. I think this is the thing stopping me moving forward in Blitzmax. I just can't figure out the FOR loop or how to use lists with it. What does it do? What needs to be setup in order to do it? I think I understand that I need them in order to create things like shots, enemy waves, things like that? Sorry to be so vague but I can only explain as much as I know (or that sinks in). Thanks |
| ||
You are GOD to the computer so you say FOR X exists and it WILL BE of values beteen 1 and 10 Do all your stuff at this value of X let X be it next vaue For x=1 to 10 .. .. next FOR X exists and it will BE A pretend name for each object in this list Do all your stuff with X as this object let X be the next obect For X:Object = Eachin MyList:Tlist .. .. Next X So if you have a Tlist of (say) Ship, then For X:Ship = Eachin Shiplist:Tlist Basicly you have to make sure that when you make a ship NewShip() you add it to a list of ships |
| ||
Basicly you have to make sure that when you make a ship NewShip() you add it to a list of ships i.e.shipList:tList = New tList ship:myShip = New myShip ship.x = 10 ship.y = 20 shipList.AddLast ship For ship:myShip = EachIn shipList ship.x:+1 Next |
| ||
A List is a datastructure. It is semanticly no different to any other type of list you might have, like a grocey list or to do list. It consists of a sequential number of items, one after the other. Each list has several ways to manipulate data. You can add an item to the list, remove an item from the list or reorder the items on the list, much like one might do to a grocery or todo list. Now because BlitzMAX doesn't have a single point of truth, there are a rediculous number of ways in which one can itterate through a List. By far the easiest is to use the EachIn itterator as mentioned in previous posts. |
| ||
Now because BlitzMAX doesn't have a single point of truth Hmm, I'm curious what that phrase means exactly? I've never heard it before. Anyhow, I'll try to say what's already been said in a slightly different manner.... This isn't actual code, just an abstract explanation. Okay, so let's say you have the following list of items (item-one, item-two, item-three, item-four) Each one of the comma separated items above is an item in the list. Now, let's say you want to do something to each of the items in that list...let's say you want to add 1 to each item. This is where the "For...Eachin" loop comes into play. The For loop let's you designate an "item specifier" and then assigns each item to that item specifier in turn, allowing you to do something to the item. So, if we name the list above "ItemList", and we name our item specifier "ItemFromList" we'd get the following code For ItemFromList = EachIn Itemlist ItemFromList:+ 1 Next The first time that loop iterates, it grabs the first item from the list (in this case that item is "item-one") and assigns it to "ItemFromList." So "ItemFromList" is really "item-one." Then we add 1 to "ItemFromList" - so we are adding one to "item-one" The second time through the list, the second item in ItemList is assigned to ItemFromList - so now ItemFromList is representing "item-two" from the list above. Again, +1 is added to it, and then the next item is pulled from the list. This process is repeated for each item in the list, and then when the loop reaches the end of the list, the loop terminates. |
| ||
Hmm, I'm curious what that phrase means exactly? I've never heard it before. It's a term taken from the book "The Pragmatic Programmer" by Andrew Hunt and David Thomas. It is defined as: "every piece of knowledge must have a single, unambiguous, authoritative representation within a system".In Eric Raymonds book "The Art Of Unix Programming" in the chapter on Design, he goes into more detail about Compactness and Orthogonality which I think explains the concepts rather elegantly. |
| ||
Thanks for the link - I'll be checking that out in a bit. |