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
Arrays can be created in a number of ways. You can specify each key and value using the bracket operator:
Copied!
$names[0] = "Fred"
$names[1] = "Kate"
$names[2] = "Sally"
$person["name"] = "Fred"
$person["age"] = 32
$person["gender"] = "male"
Arrays can also be created by Array function or the shorthand bracket notation
by simply providing the list of elements or <key, value> pairs:
Copied!
$names = Array("Fred", "Kate", "Sally")
$person = Array("name" => "Fred", "age" => 32, "gender" => "male")
$names = ["Fred", "Kate", "Sally"]
$person = ["name" => "Fred", "age" => 32, "gender" => "male"]
Here in the first example, indices are automatically assigned as 0, 1, 2
Iterating over Array Elements
Values in the array can be iterated over using foreach statements. You can access
to both keys and values or just the values:
Copied!
foreach ($name in $names)
InsertObject($Outline, "Paragraph", -1).text = $Name
foreach ($key => $value in $person)
InsertObject($Outline, "Paragraph", -1).text = $key & ": " & $value
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.
See full list of
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:
Copied!
$names = [["Fred", "Williams"], ["Kate", "Smith"], ["Sally", "Jones"]]
$SallysLastName = $names[2][1]
|