Array support in the Actions Editor

MINI LESSONS ›› Actions Editor ›› Programming Commands ››

MINI LESSON

ARRAY SUPPORT IN THE ACTIONS EDITOR

The support for arrays in the Actions Editor is implemented differently than the array support in the OpenScript language.


OPENSCRIPT

Briefly, the OpenScript array support works as follows, which demonstrates a 3 dimensional array:


to handle buttonClick

 LOCAL data[][][]

 data[4][3][1] = "Hello"

 x = 3

 request data[4][x][1]

end


ACTIONS EDITOR

The array support in the Actions Editor is quite a bit different fundamentally. At first glance it would appear to be a one dimensional array. However in reality it is an Associative Array.


Unlike a traditional array, the value inside of the brackets is not a number, it is a string. As such these are valid statements:


set data["name"] to "John Smith"

set userInfo["zip"] to 98004

set abc["age"] to 18

set files["3"] to "errorlog.txt"

Using numbers instead of Strings

You will find that you can also use a numeric value if you need.


 set files[3] to "errorlog.txt"


The absence of the quote marks around the value of 3 is not a problem as long as the value you are specifying is a number. So these two are effectively equivalent.


 set files["3"] to "errorlog.txt"

 set files[3] to "errorlog.txt"


Storing an Array inside of an Array

An interesting feature is the ability to put an entire array into an element of another array.

For example


 set userData["age"] to 35

 set userData["name"] to "John Smith"

 set employee["HR_103F"] to userData


This effectively puts the entire userData array into the HR_103F element of the employee array.


If you need to get the userData array back out of the employee array, you'd use logic like this:


 set tempData to employee["HR_103F"]

 display alert tempData["age"]


which effectively transfers the array data back into a standalone array called tempData.


Multi-Dimensional Arrays

Although multi-dimensional arrays are not technically supported within the Actions Editor, if you require this feature, it is possible to fake it.


Using the OpenScript sample (at the top) as an example, here's how you'd fake it as you write the equivalent in Actions Editor logic:


On Click

  set data["[4][3][1]"] to "Hello"

  set x to 3

  display alert data["[4][" & x & "][1]"]


The fake is achieved by treating the multi-dimensional reference (the [4][3][1]) as if it were a single string value.


Looping through all the values in the array

When using a traditional array, a Step Loop will permit you to quickly loop through an entire array, but since the Actions Editor's array is not a traditional array, a different way is needed.

Scenario:

A single dimension array named data exists which contains only numbers. The goal is to add up all of the numbers in the array.

Traditional way, using OpenScript

 total = 0

 dim = dimensions(data)

 step k from 1 to dim

   increment total by data[k]

 end

 request "The total is: " & total

Using Actions Editor

Since there is no Dimensions command in the Actions Editor (because an Associative Array is not a numbered based system, and therefore does not utilize them), another means is needed in order to loop thru all of the elements in the array. The For Each loop is the way to do that.


Writing this same logic using Actions Editor logic would look like this:


 set total to 0

 for each VAL in data

   set total to total + data[VAL]

 end

 display alert "The total is: " & total



Created with the Personal Edition of HelpNDoc: News and information about help authoring tools and software