All arrays in Cicada that are not ‘jammed’ may be resized, and there are a number of ways to accomplish this. The most straightforward method is to use the modified index operator [^...] which sets the size of a single dimension of the array, as in
A[^9]
The effect can be either to increase or reduce the size of the array.
Another way to resize an array is to use the all-indices operator [] or [*] for the last dimension of an array that is being copied to. The last dimension of the array will be resized only if necessary to prevent a mismatched-indices error. So
A[*] = B[*].x
should always work, whereas
A[<1, 5>] = B[*].x
will only work if B has 5 elements.
Finally, we can insert an array index somewhere in the middle of an array using the [+...] operator, or the [+<..., ...>] operator for multiple indices. We can also delete array indices using either the [-...]/[-<..., ...>] operators or the remove command. Here are some examples:
myArray[+5] | insert a new element before index 5
myArray[+<3, 6>] | insert 4 new elements before index 3
myArray[+<top+1, top+9>] = 17 | add 9 new elements at the end, and set them all to 17
myArray[-13] | delete array element #13
myArray[-<2, 4>] | delete array elements 2, 3 and 4
remove myArray[<2, 4>] | same -- nix elements 2, 3 and 4
New array elements are always initialized in the same way as new arrays are: for example if it is a numeric array the new elements are set to zero.
All of the operators for accessing, adding and deleting array elements also work on composite variables. The difference is that, with composite variables, these operators access, add or delete members instead of array elements. For example, if we define
threeNums :: {
a := 2
b := 5
c := pi
}
then threeNums.b is the same as threeNums[2]. If we want to delete the third member we can type either remove threeNums.c or remove threeNums[3]. And we can add two members between a and b by typing threeNums[+<2, 3>]. Those two new members will have no name, and initially will also have no data (see the forthcoming section on the void). If we enter ‘threeNums’ after all these operations Cicada will print { 2, *, *, 5 }, showing that threeNums.b is now threeNums[4]. In contrast, the old way of adding an extra member, say by typing threeNums.d :: int, sticks the new member d at the end of threeNums.
We can never reference multiple elements of a composite variable except when removing them. So threeNums[<2, 3>] is not allowed.
Last update: May 8, 2024