A Better Way to Code?

A common occurrence that I run into when coding is needing to generate a wave, whose length is not always known (can vary) and populating that wave with some data from a loop. For Example I might run something like this:

Function VariableWaveLengthCreator()

    variable n,o,LoopLength
        n=1
        o=0
    LoopLength = AVariableWhichCanChange  
   
    do
        make/o/n=(n) AWave
        AWave[o]=SomethingThatIsVariable
        o+=1
        n+=1
    while(n<=LoopLength)
end


I find I often run into issues when I do this. Is there a better way of doing this?

Clayton
If possible, it's best to make the wave once and make it big enough to handle the worst case. Then truncate it at the end.

Also, any time you are setting a point in a wave in a loop, if possible, you should change the loop to a wave assignment statement or MatrixOp command. For details execute:
DisplayHelpTopic "Waveform Arithmetic and Assignment"

For this use case I have a function called RedimWave(wv, index)
which redimensions the wave so that index is a valid row index. The new real size of the wave might be much larger than index + 1, in most cases the new size is double the old size. With that kind of quadratic growth you minimize the number of redimensions.

But now you have to store the next unwritten row somewhere. I'll do that in the wave note using NumberByKey compatible format.

So the sequence is basically:
WAVE wv = GetMyWave()
variable index = ReadIndexFromWaveNote(wv)
RedimWave(wv, index)
wv[index] = 12345
SetIndexInWaveNote(wv, index + 1)


I'm sorry that I can't share the code.