Retrieve an item from a StringList

Monkey Forums/Monkey Beginners/Retrieve an item from a StringList

MonoDesire(Posted 2015) [#1]
Hi all,

First post here. Trying out the free Monkey X. Liking it very much so far. Will probably upgrade to a Pro version quite soon.

Anyway, I have a beginner question that I have been unable to locate an answer for:

How do I retrieve/get an item from a StringList if I have the index of that item?

I'm looking for something like this (where index is an Int):

myString = myStringList.Get(index)

Can't find such method in the StringList class, nor in the List class.

I am probably missing some very fundamental piece here...

Any help is appreciated!

Best regards,
Mats


dawlane(Posted 2015) [#2]
Hi and welcome
First StringList is a link list type and doesn't use an indexing system. This mean that each node internally has the address of the next and previous item. If you actually need and index system, then use an array.
To get the index of a StringList it needs to be converted into a String array.
Global theList:= New StringList ' Global scope: Means this will be seen any where

Function Main()
	theList.AddLast("Hello")
	theList.AddLast("and")
	theList.AddLast("Welcome")
	
	' Print out the string list
	For Local i:String = Eachin theList
		Print i
	Next
	
	' Convert element to index array
	Local theNewList:String[] = theList.ToArray()
	Print "the New list is me and "+theNewList[2]
End



ImmutableOctet(SKNG)(Posted 2015) [#3]
Just to add to what Dawlane was saying, don't make a habit out of using 'ToArray', unless that's how you intend to store something for a long period. You shouldn't generate a new array every time you want to access an entry.

Also, though it's not ideal, you could hack something together like this:


At the end of the day, you should stick to an array if you have a fixed set of strings. As an alternative, a 'Stack' may be useful for some situations (Manages an array internally).


Danilo(Posted 2015) [#4]
Using an IntMap is probably something in-between using a List and an Array (in terms of speed).
Import monkey.map

Global m := New IntMap<String>

Function Main()
    m.Add(1, "Hello")
    m.Add(2, "and")
    m.Add(3, "Welcome")
	
    Print "map index 1: " + m.Get(1)
    Print "map index 2: " + m.Get(2)
    Print "map index 3: " + m.Get(3)

    For Local i := EachIn m
        Print i.Value()
    Next
End



dawlane(Posted 2015) [#5]
As ImmutableOctet(SKNG) says. Don't over do ti with the ToArray stuff as there is a penalty to pay in speed and memory. And if I remember adding items to a map (see Danilo example) has a speed cost, but accessing as map is fast.


MonoDesire(Posted 2015) [#6]
Thank you all for your prompt and very informative answers!

I realized by reading your replies here that an array is what I have should have used instead of a StringList. I need to refactor my code.

Also the IntMap is an interesting option that I will keep in mind.

Again, thank you all! ☺


ziggy(Posted 2015) [#7]
@MonoDesire: Take into account that resizing an array is also very bad in performance therms, so unless you know how many items you're going to add to the array beforehand, the best option is: Use a list to add all the strings you may need and, once you've finished adding items, convert it to an array (once) and use that instead for data access.


MonoDesire(Posted 2015) [#8]
@ziggy: That is definitely a very clever way of doing it. I will for sure keep that trick in mind. Thanks!


Nobuyuki(Posted 2015) [#9]
Or use a Stack, which handles array resizing for you...


MonoDesire(Posted 2015) [#10]
@Nobuyuki: Okay, now I feel a bit stupid... I have completely missed that there is a Stack class. I checked its documentation now, and it has the exact functionality of what I am looking for. Sorry for my ignorance... 😄