How to use Nodes?
Monkey Forums/Monkey Beginners/How to use Nodes?
| ||
I have a list with some objects in it. And I have already picked one element. But now I want to know the element which is the previous in this list. How can I do that? something like that: Class Domino Global All := New List<Domino> Field X%,Activ% Function CheckAll:Void(Pos%) For Local loc:Domino=Eachin All If Pos=loc.X loc.Activ=True loc.Previous().Activ=False ' how to do that???? Endif Next End End It would be very helpful, if the manual has more informations and samples about the LIST class. At the moment I know only this possibility: All.Find(loc).PrevNode().Value().Activ=False ...but as I'm already "inside" the list, there must be a more elegant way, or? |
| ||
You have to start with nodes in the first place, I think. I would write: Note that either version will crash if the condition is met on the first node - you probably need a test there. (Or you could keep track of two nodes always, passing the current node to the previous one before calling NextNode().) Now I come to think of it, you can indeed do it without nodes and using Eachin, by making a local prevLoc:Domino in which you store loc after each check: |
| ||
Thank you. Your second example is exactly what I did at least... It looks like there is no chance to know which node is the "current" during a "For Eachin" loop. |
| ||
Yes, I have found that sometimes you just have to accept that 'Eachin' is not enough. It happens with arrays too - you can use Eachin if you don't need to know which is current, but otherwise you must go back to For Local i:Int = 0 Until arr.Length() |
| ||
Agreed. Something like how LINQ provides a nice way to grab at objects you are looking for without specifically looping through everything would be good. I've always had to search by looping as well in Monkey-X arrays and Lists. |