Arrays

From Pengower
Jump to: navigation, search

An array variable is an ordered collection of other variables collected together under one identity. Each element in an array is given a numbered location starting at 1, i.e. 1,2,3,4… The elements in an array need not all be of the same underlying type (string or number for instance), but if different types are placed in different locations great care must be used when accessing and updating elements in an array.

Arrays are used heavily when working with relationship elements on item variables as all relationship data are returned in array form.

The basic syntax and rules governing the use of arrays are very simple and are described below.

How to Create an Array

Arrays are created through a variety of means. The following examples show the possible syntaxes:

Examples:

// an empty array
Set array = {}
// an array populated with numbers
Set array = {1, 4, 9, 16, 25, 36}
// an array populated with strings
Set array = {‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’}
// an array from a relationship element of an item variable
// assuming ‘Pets’ is a relationship element
Set array = person["Pets"]


How to Add Elements To An Array

There is a specific command designed to allow the addition of elements to an array, the Add To <arr> command. The following example shows how to use this simple construct.

Examples:

// Add 1 To array
// Add "Fido" To petnames
// Add fido To pets


How to Access and Update Elements in an Array

There is a very simple syntax to accessing array elements, similar in construct to the element access syntax used with item variables. The following examples demonstrate the syntax in use.

Examples:

 // accessing an array element
Set x = array[2]
// assigning to an array element
Set array[3] = y + 1
// looping through an array
For j = 1 To array.Count
     Set item = array[j] 
Next


How to write Array Functions

There are a number of special functions that can be performed against an array variable to provide easy access to some key functionality. The most important function and the syntax for using it were introduced in the previous example – the ‘Count’ function. Below is a list of all the functions and their syntaxes:

  • Count – get the number of items in an array, e.g. Set cnt = arr.Count
  • Max – returns the maximum value held in the array (only works when underlying data are numeric), e.g. Set max = arr.Max
  • Min – returns the minimum value held in the array (only works when underlying data are numeric), e.g. Set min = arr.Min
  • Average – returns the average (mean) value of the data in the array (only works when underlying data are numeric), e.g. Set av = arr.Average

How to write a Long Running Script

Defining a Long Running Script

To define a script as 'long running' a long running flag must be set. The first line of the script must be a comment containing the directive #LONG_RUN. This first comment will be detected during pre-processing and the script will be executed in long run mode.

To help improve the quality of user feedback there are 2 constructs available:

  • The Report command can be used to populate the message seen in the ‘progress dialog’
    • the dialog will show a message the matches the string current set to Report or ‘Processing Request Please Wait…’ if no report is given.
  • The Progress command can be set to a value between 0 and 100
    • indicating the percentage completion of the task – and the blue progress bar on the dialog will be set according to the value set to progress.


From the end user perspective the page will refresh every 4 seconds showing the above progress dialog in the work region informing the user that the processing is taking place. The user also has the option to cancel the action at any time.

Example

// #LONG_RUN
// -- script to loop through people –
Set people = Get "Person"
Set outStr = ""
Set tot = people.Count
For i = 1 To people.Count
     Set per = people[i]
     Set outStr = outStr + per["Name"] + "<br>"
     Report = "Updating " + i + " Out of " + tot
     Progress = i / tot
Next
Output = outStr
Return_Ok


Long run2.png


See Also

You might find the following topics useful:

Variables