Arrays
What are arrays?
Similar to variables, arrays are used to represent data in a task that can be different each time a task runs. But unlike standard variables, arrays can contain multiple rows and optionally multiple columns. You can think of an array as nothing more than a list, such as a shopping list, to-do list, birthday list, and so on. All items in an array, like the items in a list, have a position somewhere between first and last. Items are numbered from lowest to highest in arrays, therefore, they are accessed by number. For example, to retrieve the element in Row 2, Column 10, the following expression are entered:
%arrayName(2,10)%
One and two dimensional arrays
An array can be used to store a list of data read from a text file or other system containing customer data. For instance, if there are 10 customers, the array needs to have 10 rows. If the data consisted of first name, last name, and company name, it needs 3 columns and needs to be a two dimensional array. An array can also be a simple list of text or numeric values (for example, multiple rows of data, only one column), this is called a one dimensional array.
An array with only one dimension is linear. In other words, it contains a list of data that can be referenced by a number. For example if one dimensional array named myArray has 3 values, then the syntax is:
MyArray(1) = value1
MyArray(2) = value2
MyArray(3) = value3
In a two dimensional array, you have both rows and columns, such as a spreadsheet. You need to reference a cell by row and column, like the expression below which references row 1, column 5.
%MyArray(1,5)%
Creating and setting arrays
The Array action contains individual activities that allow you to create or modify an array. To create an array, use the Array - Create activity. In the properties of this activity, give your array a name, choose whether you want your array to be a one, two, or three dimensional array and optionally, set its values. After creating an array, you can set its values in any task step using the Array - Set activity or you can resize an array using the Array - Resize activity.
Examples
- Copy and paste the sample AML code below directly into the Task Builder Steps Panel.
- To successfully run the sample code, update parameters containing user credentials, files, file paths, or other information specific to the task to match your environment.
Example 1
The following sample task associates each array with a color:
<AMARRAY NAME="MyArray" TYPE="TEXT" ROWS="3" />
<AMVARIABLE ACTIVITY="set" VARIABLENAME="MyArray(1)" VALUE="Red" />
<AMVARIABLE ACTIVITY="set" VARIABLENAME="MyArray(2)" VALUE="Blue" />
<AMVARIABLE ACTIVITY="set" VARIABLENAME="MyArray(3)" VALUE="Green" />
<AMSHOWDIALOG>%MyArray(1)%; %MyArray(2)%; %MyArray(3)%</AMSHOWDIALOG>
Example 2
The following is a more complex task that uses functions and performs loops (requires a folder called c:\test\ with a few files in it):
<AMVARIABLE NAME="thefilename" />
<AMVARIABLE NAME="counter" />
<AMARRAY NAME="MyArray" ROWS="%FileCount('c:\test\')%" TYPE="text" />
<AMLOOP ACTIVITY="folder" FOLDER="c:\test\" RESULTVARIABLE="thefilename" />
<AMLOOP FROM="1" TO="%UBound(myarray, 1)%" RESULTVARIABLE="counter" />