Contents - Index


Array (function)

Array(nArray,nIndex) : string

nArray - must be a number between 1 to 50 that identifies the "in memory" array being used. GSAK currently only supports a maximum of 50 arrays. 
nIndex - the index (element number) of the corresponding array

For example, to set 13th element of the "in memory" array number 2 the syntax would be:
 

Array(2,13) = "some data"

 
To access the same data in the array, is very much like any other function:
 

$data = Array(2,13)
Msgok msg=$data

 
You can have up to 2 billion elements in any array but you must first initialize your array and set the maximum number of elements it can contain. Initializing an array sets the number of elements and clears each element (they are all set to "" - empty string)

To initialize an array you just need to use the reserved index number of 0 (zero) and set the required number of elements. For example, if we want the inline memory array number 1 to have 1,000 elements the syntax would be:
 

Array(1,0) = "1000"

 
Here is an example of saving the cache code and name (in two separate arrays) for your whole database (or current filter)
 

# Initialize required array size to number of records in our grid
array(1,0) = "$_Count"
array(2,0) = "$_Count"
$x = 0
Goto position=top
while not($_eol)
  $x = $x + 1
  array(1,$x) = $d_code
  array(2,$x) = $d_name
  goto position=next
endwhile
Goto Position=Top


Arrays also support access to every element, and sorting.

1. Access to the full array

Using the reserved index value of -1 (minus 1) will give you access to the complete array (setting and getting)

For example, to set every element in array number 1 to the string "xxx":
 

Array(1,-1) = "xxx"

 
To get the contents of array 1 as a concatenated string of all the elements
 

$fullarray = Array(1,-1)

 
2. Sorting arrays

Use the reserved index of -2 (minus 2) to force the contents of the array to be sorted.

You can change the behaviour of the sort by using a combination of flags.

Sequence - A = Ascending, D = Descending (default is A)
Case - I = Ignore case, R = Respect case (default is I)
Type - S = string, N = Numeric (default is S)

These flags can be in any order and are case insensitive. If you don't enter any flags, (or leave out an individual item) then the default values are used for each item.

The "type" flag has been added to help you sort string data (remember, arrays always contain string data) in a true "numerical" way:

For example, take the following macro. If we use the defaults, because we are sorting "strings" we get our data sorted in string order:
 

array(1,0) = "5"
array(1,1) = "9"
array(1,2) = "100"
array(1,3) = "13"
array(1,4) = "50"
array(1,5) = "1001"
array(1,-2) = ""
$x = 1
$d = ""
while $x <= 5
$d = $d + array(1,$x) + $_NewLine
$x = $x + 1
EndWhile
msgok msg=$d

  


To get the array in the correct numerical order we just need to change our sort line to:
 

array(1,-2) = "N"

  

 
If we wanted the data in reverse sequence, we change our sort line to:
 

array(1,-2) = "ND"

 


Related: List()

Alpha List         Category List