Question regarding Replacing an Array

Hi all,

I have a small question about “best practices”.

I’m making a “preset” that refers to some indexes of an array that will be loaded dynamically.

I can reserve a hundred slots in the array easily: ~a = Array.fill(100, {0});
But in the “loading section” of the code, I am adding to ~a: ~a = ~a.add(Array.fill(100, {1}));

The problem here is that I now have a 200-slot array with 100 dummy values.

I could simply overwrite the whole array, by repeating the initial line - but if I don’t add a conditional statement that says “after the first time overwriting the ~a array, switch to “adding” to the ~a array.” I’m wondering if there is a more concise approach, though.

Thanks!

I’m gathering that those initial 0s are placeholders that you want to overwrite before potentially adding new values to the end of the array and thereby increasing the array size. So I wonder, do you need those initial 100 placeholder values at all? If not, you can initialize an empty array and then add to it as needed

~a = [];

// Then as needed:
~a = ~a.add(whatever);

But if your case is more complex than this maybe you can describe in more detail what you hope to achieve?

1 Like

Well, I think I needed the original placeholders - I was referencing buffers in advance of being loaded - but the conditional logic solution works well for a few extra reasons.