I thought of 2 possible syntaxes declare an array:
variable_type array_name[starting_index to
last_index].
like C, or
declare array_name[starting_index to last_index] is variable_type.
Both indices are required; they set the lower-bound and upper-bound for the array. Only zero (0) and positive integers can be used. I also came up with 2 ways to declare multi-dimensional arrays:
array_name[starting_index to last_index | starting_index2 to last_index2 | ...]
and
array_name[starting_index to last_index ; starting_index2 to last_index2 ; ...]
I am leaning towards the latter. Whether '|' or ';' is used in implementing array declarations, the same symbol should be used to offset dimensions when using arrays. For now, I'll use the semicolon. Here are some examples of using an array element:
x = arr[5] eos
y = arr2[7; 0] eos
arr2[4;5] = a_bc eos
Why am I using semicolons instead of commas? Well, suppose you want to access 2 or more elements at once. Here are some examples of how that's done:
(a, b) = arr[0, 4] eos
arr[1, 6] = -8 eos
In the first example, arr[0] is set to the value of variable a and arr[4] is set to the value of variable b. In the second example, arr[1] and arr[6] are set to -8. What would multi-dimensional array access look like with this syntax? Here's an example:
(a, b) = arr2[x; 0, 2] eos.
where x is an integer variable
This is equivalent to
a = arr2[x; 0] eos
b = arr2[x; 2] eos
To assign values to elements of an array in one statement, use the array built-in function. Here are some examples:
arr[0, 3] = array("first", "name).
arr2 = array(1 to 5).
Note that if the element specification is left off, Phoenix will try to load the array starting with the index with the lowest value. By the way, You can get that value by using arr2->lower(1) . The highest index can be retrieved using arr2->upper(1). The dimension number (starting with 1) is the argument for both methods. arr->lower(2) gets you the lower bound index of the second dimension of array arr.
What if you don't want to type arr[0, 1, 2, 3] ? The to range operator can be used instead: arr[0 to 3]. You could even "slice" an array, such as arr3, like this:
arr3[0 to 3; 1 to 4, 5 to 6, 8; 6] = array(...
Associative arrays? Phoenix has them, too. If you want to index a dimension with strings instead of numbers, declare the array using the assoc keyword, like the following example:
declare names[assoc] is string.
Accessing such an array is straightforward:
names["last"] = "Hemmingway" eos
names["first", "last"] = array("Ross", "Albertson")
eos
first = names["first"] eos
You can also mix numerical and string indices in multidimesional arrays like this:
declare personal[1 to 10; assoc; 0 to 9] is string.
Here's an example of accessing an element from such an array:
ssn = personal[2; "data"; 5] eos