Home > Language Reference > Introduction to the Language > Using Arrays

Using Arrays

An array is a variable that holds multiple values of the same data type (integer or string).

An array can be used to store data read from a text file, an Excel file or other system containing data. An array is a simple list of text or numeric values (multiple rows only, one column), this is called a one dimensional array and WinTask supports only one dimensional arrays. If there are n columns, you need to use one array for each column. For example, if there are 10 customer names to process, the array would need to have 10 rows. If the data consisted of firstname, lastname, and company name for 10 customers, you would need a 10 rows array for the firstnames, a second 10 rows array for the lastnames and a third 10 rows array for the comany names.

You give an array a name and set its size by the Dim statement. The name of an array ends with a $ character if the array contains strings, does not end with a $ character if the array receives integer values. For example:
Dim count(10)
declares an array with 10 integer elements.
Dim MyName$(100)
declares an array with 100 strings.
The Dim statement must be written at the very beginning of the script where it is used.

An array contains a list of data that can be referenced by a number (called an index). The first element of an array is at index 0. You set a value in an array using the = operator:
Dim count(10)
    count(0) = 20
    count(1) = 20 + 30
    count(2) = 100
    count(3) = count(0) + count(2)

Dim MyName$(3)
    MyName$(0) = "First element"
    MyName$(1) = "Second element"
    MyName$(3) = "Third element"

Array manipulation capabilities

tab1$() = tab2$() ' the entire contents of the 2nd array are copied into the 1st. If the 1st array has more entries than the 2nd, the remaining ones are unchanged.
 
tab$()="blue"     'All the entries of the array are initialized with "blue"
tab$()=""           'All the entries are emptied

If a$()=b$() then     'Compare the contents of two arrays. This condition is true only if the arrays have the same number of elements AND all corresponding entries in both arrays are equal.

Loop until the first empty string of the array:
Dim tab1$(10)
    tab1$(0) = "First element"
    tab1$(1) = "Second element"
    tab1$(2) = " Third element"
    i=0
    repeat
        msgbox(tab1$(i))
        i = i + 1
    until tab1$(i)="" or i=10


IMPORTANT


When an array is a parameter in a function, the variable name for the array ends with (), for example:
WriteExcel ("myexcel.xlsx","A1",tab$())
The variable name for the array is tab$().

See also

Using Functions
Constants and Variables
Expressions and Operators
Global and Local Variables
Visibility of Variables
Script Structure