If the call to
MIDI.read() is a blocking call (it stays in there until it has a value) then you are pretty close:
const int NumberOfInputs (42);
for (int index = 0; index < NumberOfInputs; ++index)
You can simplify you handler by post incrementing the learnindex counter as you use it. (Post increment will happen after the value has been 'used')
void MyHandleNoteOn(byte channel, byte pitch, byte velocity)
learn[learnindex++] = pitch;
When you increment a variable on its own line its usually better to pre increment it, because you aren't using the value and it might result in quicker execution. However remember you are increasing the value before using it, so if you use it as above you end up accessing the array element one higher than you intended to!
int learnIndex = 0;
++learnindex; // Pre-increment it to 1
learn[learnIndex++] = 1; // Set element 1 to 1.
learn[++learnIndex] = 3; // Set element 3 to 3.