Onetastic Macro Documentation >
>
Arrays Arrays
Variables can store a single value or a set of values. Variables that store
a set of values are of Array data type. Arrays can store an unbounded amount of
<key, value> pairs. The values are always accessed by providing the key (known as index).
New elements can be added to arrays by assignment and specifying the a key that doesn't
exist in the array:
Creating Arrays
$Names[0] = "Fred"
$Names[1] = "Kate"
$Names[2] = "Sally"
Arrays can also be created by Array function by simply providing the list of elements:
$Names = Array("Fred", "Kate", "Sally")
Here the indices are autmatically assigned as 0, 1, 2
Iterating over Array Elements
Values in the array can be iterated over using foreach statements
foreach ($Name in $Names)
InsertObject($Outline, "Paragraph", -1).text = $Name
Array keys are of string or numeric types and they don't have to follow any order.
Array functions
There are a several functions that generate and consume arrays. QueryObjects and QueryText
functions return arrays of objects. String_Split function splits a given string into an array
of strings, while Array_Join function reverses this. Array_Length function will return the
number of elements in an array.
Using an array as a stack or a queue
Array_PopFront and Array_PushBack functions can be used as equivalents of dequeue and
enqueue operations on a queue data structure. Similarly Array_PushBack and Array_PopBack
functions allow an array to be used as a stack, providing simple push/pop functionality. Finally
there is an Array_PushFront function to insert an element at the beginning of an array.
Multi-dimensional Arrays
Arrays can be multi-dimensional if they contain other arrays:
$Names = Array(Array("Fred", "Williams"), Array("Kate", "Smith"), Array("Sally", "Jones"))
$SallysLastName = $Names[2][1]
|