[JW Lua] Tables getting reset?

Emile Meuffels e.meuffels at gmail.com
Wed Sep 16 13:48:06 CEST 2020


Hi all,

I am trying to write a program which calculates the slide positions for a
trombone and calculates the total slide length for the selected notes. As
there are some notes with alternative positions, I want to create a table
where each row has a possible combination of positions. In column 0 there
will be the total slide length per row, so I can find the combination with
the smallest total slide length.
If there is an alternative position possible for a note, it creates
new rows and copies the previous values. But for some reason, when I try to
copy a cell in the next for-loop, that value is always nil, although it was
filled in the previous loop...

I am a total newbie to Lua and to some degree to programming in general, so
I might not be doing this the most elegant way, but if I insert some print
lines, the program does everything I want. So ugly as it might be, it
works, it just doesn't seem to remember the values...

I have added the relevant code under this message. Any suggestions are
appreciated!

All the best,

Emile

column = 1
totallength = 0
i = 1
c = 1
imax = 1
k = 1
for entry in eachentrysaved(finenv.Region()) do
    local highestnote = entry:CalcHighestNote(nil)
    if highestnote then
        midi = highestnote:CalcMIDIKey()
                -- the slide position is calculated, deleted here for
clarity
                for i= 1, imax do
                    table[i]={}
                    table[i][c] = position
                    -- the slide length is calculated, deleted here for
clarity
                    table[i][0] = totallength
                end

                if Alternation(midi) then -- determines if there is an
alternative position
                    for k = imax+1, imax*2 do -- new rows are being created
                        table[k]={}
                        for d = 1, c-1 do -- and the input of the last rows
is copied
                            table[k][d] = table[k-imax][d] -- this should
copy the value of the previous rows to the new rows, but it always give a
nil value...
                        end
                        -- the alternative position is calculated, deleted
for clarity
                        table[k][c]= position -- here the new value is added
                        -- and the new length is calculated, deleted for
clarity
                        table[trumpet][k][0] = totallength
                    end
                    imax = imax*2
                end
        end
    c = c + 1 -- end
end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://jwmusic.nu/pipermail/jwlua_jwmusic.nu/attachments/20200916/572f4bd2/attachment.htm>


More information about the JWLua mailing list