As also specified in Data types in Sling, Sling includes a build-in data type named vector. This is a dynamic array data structure, which holds an array of elements of various types, where each element is indexed with a zero-based integer.
Declaring and creating vectors
Vectors are declared using the Sling generics mechanism and syntax. This way the vector can be declared for various data types, as follows:
var strings = new vector<string>
var objects = new vector<object>
The former would declare a vector that can contain strings, the latter a vector that can contain any object type. The latter is also the default, and could be expressed simply as:
var objects = new vector
Adding elements
Adding of elements to a vector can be done easily using the += operator. This will grow the size of the vector, and will add elements to the end of the vector:
strings += "first string"
strings += "second string"
Accessing data
The size of a vector (the number of elements it currently contains) can be retrieved using the SIZE operator:
var numberOfElements = SIZE strings
Retrieving of individual elements can be done using the subscript operator:
var firstEntry = strings[0]
var secondEntry = strings[1]
By combining the two, you can loop through all elements using the for loop:
for(var n=0; n<SIZE strings; n++):
PRINT "ELEMENT: " .. strings[n]
However, you can more easily (and therefore, preferrably) also use the foreach loop:
foreach string in strings:
PRINT "ELEMENT: " .. string