Issues with for loop?
-
Just trying to use a for loop to print out the elements at every index of an array,
but the "i" variable in the code inside the loop is not working.Am I writing something wrong here? It just tells me that "i" is an undefined parameter.
-
@VirtualVirgin that means that your noteOnOffState array doesn't contain 128 values
-
Is
noteOnOffState
a MIDI list? -
@d-healey said in Issues with for loop?:
Is
noteOnOffState
a MIDI list?Not in this case, just an array.
-
@VirtualVirgin try doing this instead:
function onController() { Console.print(trace(noteOnOffState)); }
but it may well be that noteOnOffState isnt defined or visible to your call back.
-
@VirtualVirgin said in Issues with for loop?:
Not in this case, just an array.
In that case instead of using
128
you can usenoteOnOffState.length
and then the loop will never exceed the number of elements in the array. -
@d-healey said in Issues with for loop?:
@VirtualVirgin said in Issues with for loop?:
Not in this case, just an array.
In that case instead of using
128
you can usenoteOnOffState.length
and then the loop will never exceed the number of elements in the array.I just tried that while you were tying this, but I still get an error:
Here is the array declaration:
-
@VirtualVirgin Don't use global, use
const
. -
@Lindon said in Issues with for loop?:
@VirtualVirgin try doing this instead:
function onController() { Console.print(trace(noteOnOffState)); }
but it may well be that noteOnOffState isnt defined or visible to your call back.
I did use that earlier, but I don't know how to append it to use strings in between each returned index value.
-
@d-healey said in Issues with for loop?:
@VirtualVirgin Don't use global, use
const
.Well somehow it works now once I added the
i + ": "
to the print command.
I didn't change the variable type yet.
const variable will be available across the different callback scripts?
-
@VirtualVirgin A
const
will be available throughout the MIDI processor in which it is declared. It won't be accessible from other MIDI processors. -
@VirtualVirgin It's still saying undefined for that variable. You haven't solved the problem.
Where are you populating the array?
-
@d-healey said in Issues with for loop?:
@VirtualVirgin A
const
will be available throughout the MIDI processor in which it is declared. It won't be accessible from other MIDI processors.And does the scope of a global include the other MIDI processors (out of curiosity)?
-
@VirtualVirgin said in Issues with for loop?:
And does the scope of a global include the other MIDI processors (out of curiosity)?
Yes, but there is usually a better method. Global variables lead to less modular code, which is more difficult to maintain and debug. I have yet to use them in any project.
-
@d-healey said in Issues with for loop?:
@VirtualVirgin It's still saying undefined for that variable. You haven't solved the problem.
Where are you populating the array?
No, no! It is working properly.
Those "undefined" indexes are not written to yet.
Only those which have been written to have a value (1 or 0 here).If you see, index 60 has a value of 1, meaning that middle C has note on.
If it matters later, I could initialize the array with all 0s, but I don't know that I need that data yet.
-
@VirtualVirgin said in Issues with for loop?:
Those "undefined" indexes are not written to yet.
The error you created this thread for is caused by the values being undefined
In this situation you need to filter out undefined values, this can be done with a simple
if
inside the loop.if (!isDefined(noteOnOffState[i]) continue;
If you're just storing a 1 or 0 in the array then you should probably use a MIDI list.
-
@d-healey said in Issues with for loop?:
@VirtualVirgin said in Issues with for loop?:
Those "undefined" indexes are not written to yet.
The error you created this thread for is caused by the values being undefined
In this situation you need to filter out undefined values, this can be done with a simple
if
inside the loop.if (!isDefined(noteOnOffState[i]) continue;
If you're just storing a 1 or 0 in the array then you should probably use a MIDI list.
That error was coming up for trying to print from the loop.
That error disappeared when I added
i + ": "
to the Console.print()The console is printing the loop and the error is no longer in the console.
-
Without
i + ": " +With
-
The error is telling you that the thing you are trying to print is undefined. When you add
i + ": " +
then you are no longer printing something that is entirely undefined. - However as you can see from the console output the array part of it is still undefined. -
@VirtualVirgin
If you do not want to initialize your array with 0 or 1, you could also initialize it with -1.