Creating Arrays in Opus

Arrays are variables but they differ from other variables in that contain more than one value at a time. There are two ways that you can create Array variables in Opus and then add the contents, they are:

(i) Using the New Variable dialog and Set Variable Array action – this method allows you to create Array variables by filling-in information in familiar dialog boxes.

(ii) Using OpusScript – this method creates arrays using the OpusScript programming language.

Whichever method you use to create an Array variable, the information can be displayed on a page in a Text object so the user can see the contents. However, often arrays are created in OpusScript purely to calculate other functions in a script and may never be shown to or used by a user.

Note:
For a further discussion on Array variables and other types of variables, such as Publication and Page variables – see Types of Variables for more information.

Creating an Array and adding contents with dialog boxes:

1. Use the New Variable dialog to create an Array variable. This will create the Array itself but it does not actually allow you to fill-in the values for each element that make up the Array variable. In other words, you have created the structure but not the contents of an Array variable.

Note:
For a full description on creating variables – see Creating Variables for more information.

2. Use the Set Variable Array action to add the values to an element of an Array variable. The Set Variable Array action only allows you to set the value of one element at a time. Therefore, if you have 30 elements in an array, you will have to use the action 30 times.

Note:
For a full description on using this action – see Set Variable Array action for more information.

3. The Set Variable Array action is used to add the initial value of an element in an Array variable and also whenever you want to change the value of an element. You can change the values of individual elements at any time and this will not affect the contents of other elements in the Array variable.

Creating an Array and adding contents in OpusScript:

1. Use a Script Object to add the Array variable.

2. In the Script Object you must use the correct syntax to create the array. For example, if you want to create an array named quizFeedback that will be used to provide feedback for questions in a quiz, do the following:

  quizFeedback = new Array()

Note:
This will create the new Array variable but it currently has no elements or values for each element. In OpusScript you can create Array variables that specify how many elements it contains – see Array Syntax for more information.

3. To add the initial value to an element in an Array variable, name the array element and add the value. Continuing the example above, we will add the value to the first two elements of the Array variable named quizFeedback:

  quizFeedback[0] = "Option C is the correct answer"

  quizFeedback[1] = "Option B is the correct answer"

Note:
In OpusScript you could add all sorts of values: strings (i.e. text), as in the example above; numbers or calculations that will result in a number; or variable names whose contents will be placed in the array. If you are familiar with programming, this will be straightforward.

4. To change the initial value of an element to a new value simply repeat the process in point 3 above at some other point in your script.

Related Topics:

Types of Variables

Creating Variables

the Set Variable Array action

Overview of Variables in Opus