re-sizing an array
-
is there any way to change the size of an array?
in the other programming language i know, processing, it is done like this// declare array
int[] name;//could also be declared like this, but you have to have the size with this one
int[] name = new int[14]//main loop
void draw() {
//new size
name = new int[23];
}is there anything similar in FUZE?
-
Arrays expand dynamically in FUZE. So, you can do:
myArray = [] for i = 0 to 10 loop myArray[i] = 1 repeat myArray[15] = 1
And Fuze will create elements up to the 15th, filling each element in with void values. This will give you some trouble if you try do things with that value, such as printing for example.
You can also do:
array myArray[10] myArray[15] = 1
Works exactly the same way!
-
@Dave what about making an array size smaller? how would you do that?
-
I've shared a project with ID X7R53MND1Z
That contains a function called
splice
that you could copy into your project.You could call it like this:
output = splice([array], positionToRemove, numberOfElementsToRemove, [array with elements to add in stead])
output.result // contains the modified arrayso for example
output = splice([0,1,2,3,4], 2, 1, [])
would lead to
output.result = [0,1,3,4]
so from [0,1,2,3,4] the element at position 2 (coincidentally also the value 2) is removed (1 in the function call means remove 1 element, the empty array at the end just means you don't want to replace the value with anything else)Splicing gives you flexibility in how you want to modify the array. However it does work with making a copy of the array.
I hope that made sense.
-
I did find some small bugs that I've fixed (update is pending for review):
Both with adding elements to the end and with inserting at the start of the array did not go well.