Loop - Array

Declaration

<AMLOOP ACTIVITY="array" DIRECTION="text (options)" ARRAY="text" RESULTVARIABLE="text" DATASET="vertical" STARTINDEXZERO="YES/NO" />

Related Topics   

Description

Loops through an Automate Desktop array, populating the specified variable with the current element. This activity supports looping the array “horizontally” (row by row, column by column) or “vertically” (column by column, row by row). It also supports any size array of any dimensions and can optionally populate a dataset with the array name and current index.

Practical usage

Used to perform one or more operations on each element of data in an array.

Parameters

General

Property Type Required Default Markup Description
Array Text Yes (Empty) ARRAY="myarray" The unique name of the array to loop. The array must be created and populated in a previous step using the Create Array activity and the Set Variable activity.
Populate variable with current element value Text Yes (Empty) RESULTVARIABLE="varname" The name of an existing variable that should be populated with the array's next data element on each successive loop.
Store array indices in dataset Text No (Empty) DATASET="ArrayIndex" If enabled, specifies the name of a dataset to be created and populated with the array name and current index. This parameter is disabled by default.

Start index with 0

Yes/No No No STARTINDEXZERO="YES" If selected, the index value will start with 0. If disabled (default) the index value will start with 1.
Loop order (multi-dimensional arrays) Number No First dimension first (horizontal) DIRECTION="vertical"

The direction of the loop. The available options are: 

  • First dimension first (horizontal) - Loop is performed row by row, then column by column.
  • Last dimension first (vertical) - Loop is performed column by column, then row by row.

Description

Error Causes

On Error

Example

NOTE:
  • 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.

Description

This sample task demonstrates two-dimensioned arrays with loops, as well as the Ubound function which returns the highest index of a variable that holds an array of values. In order for this task to function properly, a folder named "test" must exist in the root of c:\ (for example: c:\test\) with a few files in it. The Array has a row for each file in the folder. Three columns hold the file name, size and date.

Copy
<!-- Sample With Loops using two-dimentions (requires a folder called c:\test\ with a few files in it). Array has a row for each file in the folder. 3 colums hold filename, size and date. -->
<AMVARIABLE NAME="thefilename" VALUE="" />
<AMVARIABLE NAME="counter" VALUE="" />
<AMARRAY NAME="myarray" ROWS="%FileCount('c:\test\')%" COLUMNS="3" DESCRIPTION="array to hold filename size and date" />
<AMLOOP ACTIVITY="folder" FOLDER="c:\test\" RESULTVARIABLE="thefilename"><AMINCREMENTVARIABLE RESULTVARIABLE="counter" /><AMSET VARIABLENAME="myarray(%counter%, 1)">%thefilename%</AMSET><AMSET VARIABLENAME="myarray(%counter%, 2)">%FileLen(thefilename)%</AMSET><AMSET VARIABLENAME="myarray(%counter%, 3)">%FileDateTime(thefilename)%</AMSET></AMLOOP>
<AMVARIABLE ACTIVITY="increment" RESULTVARIABLE="counter" />
<AMVARIABLE ACTIVITY="set" VARIABLENAME="myarray(%counter%, 1)">%thefilename%</AMVARIABLE>
<AMVARIABLE ACTIVITY="set" VARIABLENAME="myarray(%counter%, 2)">%FileLen(thefilename)%</AMVARIABLE>
<AMVARIABLE ACTIVITY="set" VARIABLENAME="myarray(%counter%, 3)">%FileDateTime(thefilename)%</AMVARIABLE>
<AMLOOP ACTIVITY="end" />
<AMLOOP TOTALLOOPS="%UBound(myarray, 1)%" RESULTVARIABLE="counter"><AMMESSAGEBOX>Filename: %myarray(counter, 1)% File Size: %myarray(counter, 2)%File Date: %myarray(counter, 3)%</AMMESSAGEBOX></AMLOOP>
<AMSHOWDIALOG>Filename: %myarray(counter, 1)%File Size: %myarray(counter, 2)%File Date: %myarray(counter, 3)%</AMSHOWDIALOG>
<AMLOOP ACTIVITY="end" />