About Monkey 2 › Forums › Monkey 2 Programming Help › Loop through only part of list?
This topic contains 4 replies, has 4 voices, and was last updated by
nerobot 2 years, 4 months ago.
-
AuthorPosts
-
November 20, 2016 at 1:39 pm #5273
I am creating a listbox and want to use a list. What I do not know how to do is to loop through only a part of the list.
Say from 10(the top of the window index) to 30(the bottom of the window index)
What I do not know now is how to get the contents from a list item.
drawtext Mylist.item[10]
I do know how to loop through a complete list using eachin.
November 20, 2016 at 2:22 pm #5274I am looking on google and see that it probably is not possible with linked lists. I will start to look at stack and else I wil use a regular array.
November 20, 2016 at 2:38 pm #5275To go through a complete list:
[/crayon]Monkey123456[crayon-5cb9d2fd680d2577045168 inline="true" ]For Local itemInstance := EachIn myListitemInstance.x = 10itemInstance.y = 25NextThere is no simple way to go trough part of the list. list are made to go from the first to the last or vice versa. you can convert it “ToArray” and then index it that way but converting it to array is slow. or you can use an index variable to find the ones you want to process:
[/crayon]Monkey12345678910[crayon-5cb9d2fd680d8877614712 inline="true" ]Local i:int = 0For Local itemInstance := EachIn myListif i > 3itemInstance.x = 10itemInstance.y = 25EndIfi += 1Nextor
[/crayon]Monkey1234567[crayon-5cb9d2fd680dd967029055 inline="true" ]For Local itemInstance := EachIn myListif itemInstance.y > 100itemInstance.y = 0EndIfNextNovember 20, 2016 at 2:48 pm #5276the more complex version! you can replicate what happens to eachin loop
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243#Import "<std>"Using std..Class myItemsField ID:IntField content:StringMethod New(i:Int,s:String)ID=icontent=sEndEndFunction Main()Local alist:List<myItems> = New List<myItems>alist.Add(New myItems(1,"One"))alist.Add(New myItems(2,"Two"))alist.Add(New myItems(3,"Three"))alist.Add(New myItems(4,"Four"))alist.Add(New myItems(5,"Five"))alist.Add(New myItems(6,"Six"))alist.Add(New myItems(7,"Seven"))alist.Add(New myItems(8,"Eight"))alist.Add(New myItems(9,"Nine"))alist.Add(New myItems(10,"Ten"))Local iterator:=alist.All()While Not iterator.AtEndWhile iterator.Current.ID < 4iterator.Bump()WendLocal value:=iterator.CurrentIf value.ID > 7 ContinuePrint value.ID+ " " + value.contentiterator.Bump()WendEndIf you wanted you could implement a custom iterator too, but in the real world… just do what Jesse suggested…
November 22, 2016 at 2:14 am #5330In the latest example we got endless loop. Should replace ‘continue’ with ‘exit’.
-
AuthorPosts
You must be logged in to reply to this topic.