A List represents a one-dimensional array of values. Each element in the List can be of any data type (including List) or Undefined.
When you declare a variable to be of type List, it is initially empty. List values can be obtained by using a List literal or by executing a function that returns a List value (for example the function Str.Elements()).
List namesList = { "Amy", Naomi", "Arnold, "Miki", "Blanca" } List values = { namesList, ( 21 + 6 ), Date.Now(), Undefined }
In the above example, a List literal is created by defining a set of values, surrounded by braces and separated by commas. Note that the elements in the List need not be literals; they can be other Lists, expressions, or functions.
The expansion operator ( @ ) can be used to reference all elements contained in a List, rather than the List itself. This is useful to insert the contents of a List into another List. For example:
List tempList = { 1, "Joe" } tempList = { @tempList, 34, "dog", TRUE } Echo( tempList ) // output is "{1,'Joe',34,'dog',true}"
In this example, tempList is assigned a new value consisting of the current contents of tempList with the addition of three elements. Without the expansion operator, the first element of the new List would be the original List. With the expansion operator, the original List's elements are inserted as separate elements in the new List. Note that the empty list expands as expected, while one should be careful that a List value which is Undefined is not expanded. For example:
List v = {} Echo( { @v, 34, @v, "dog", v } ) // output is "{32,'dog',{}}" v = Undefined Echo( { @v } ) // causes script crash
The elements contained within a List can be referenced using the index operator ( [ ] ), where the first element in the List is element 1. For example, the fourth element of the List variable myList can be obtained using the following syntax:
Dynamic theValue = myList[ 4 ]
The range operator ( : ) can be used to define a range of elements to form a subset. The following example illustrates:
List words = Str.Elements( "Now is the time", " " ) List firstThree = words[ 1:3 ]
In the above example, firstThree will contain the value:
{ "Now", "is", "the" }
You can use an open-ended range, for example, [ : 6 ] specifies a range from the beginning through the 6th element, and [ 3: ] specifies a range from the third through the last element.
You can also refer to List elements using negative numbers, where the last element of the List is -1. For example, myList[ -2 :-1 ] returns a List containing the last two elements of myList.