Summing the elements of an array?
-
Hi,
I am trying to sum all the elements of an array in real time?
We don't know how many elements there is going to be in the array at any given time. For example; an array containing the note numbers of all the keys that are being held down. The objective is to always know the sum value of the note numbers in the array.I'm using a simple but inefficient way, (see example below). It sorta works, but It's hardly an efficient way to do it. And it imposes limits.
I believe there is an efficient way to script it with a loop, but I was not able to put it together.
little help please?:folded_hands_medium-light_skin_tone:
A = [0]; sumA = A[0] + A[1] + A[2] + andSoForth to include all elements in the array.
-
@RastaChess Use a loop. But why do you want to do this?
-
@RastaChess
const array = [1, 23, 4, 65, 78]; reg sumA = 0; for (n in array) sumA += n; Console.print(sumA); // or for (i = 0; i < array.length; i++) sumA += array[i];
-
@ulrik Thank you very much. I used your first example. It works great!
Now I can delete, 40 sums from my script.
-
@d-healey I'm attempting to write a legato_glide script for a monosynth. I know, don't laugh... It's actually about 98% working. But it's all based on arrays and its very convoluted. I does the job pretty well though. I just have one or two wrinkles to iron out.
I 'm summing the elements of an array to calculate the accumulated value of the intervals derived from the oldRoot to newRoot interval movements during a legato run.
Your tutorials have helped me learn a lot, thank you for those.
-
@RastaChess It sounds unnecessary, but I don't know without seeing the code.
Here's a glide script I wrote - https://github.com/davidhealey/HiseUtilityScripts/blob/master/modules/PortGlide.js
-
@d-healey I supposed it could be. It's an important part of this iteration of the script though.
I downloaded your script months ago for research. Thank you for making that available. However, I needed mine to work in a slightly different manner, I couldn't find a better alternative, so I decided to write my own.
I went with arrays, so far so good.
-
@d-healey Moving average would be one thing. Is there a better way to do value smoothing in HISE?
-
@aaronventure Not something I've looked into, usually anytime I need smoothing I'm using a modulator that handles it.