if a[1] ~= nil then
if a[1].b[1] ~= nil then
if a[1].b[1].c[1] ~= nil then
end
end
end
Or
if #a > 0 then
If #a[1].b > 0 Then
etc..
I have no real idea how you can sanitize multidimensioned arrays in an easier way.
The best thing you could to is simply populate the first array with a record value of how many records are stored in the follow up cells, in that way you will never get nil values :
a[1].b[1].c[1] = 0
a[1].b[2].c[1] = 0
a[1].b[3].c[1] = 0
a[2].b[1].c[1] = 0
a[2].b[2].c[1] = 0
a[2].b[3].c[1] = 0
If val ~= 0 then
a[1].b[1].c[2] = val
a[1].b[1].c[1] = a[1].b[1].c[1] + 1
end
If val2 ~= 0 then
a[1].b[2].c[2] = val2
a[1].b[2].c[1] = a[1].b[2].c[1] + 1
end
for _ = 2, a[1].b[2].c[1] do
print(a[1].b[2].c[_])
end
If the array in the for-loop does not contain anything on node 2, the for loop will simply not execute.
Thanks for the code, it`s given me something to think about though.
In the mean time I had an idea with the pcall() lua`s protected call function that seems to catch this for a bool test:
if pcall(function() local a = a[1].b[1].c[1] end) then
--do this as the table was valid
else
--do this as the table returned an error such as: attempt to index field '?' (a nil value)
end
Here pcall() calls an anonymous function where the table in question is simply assigned to the local variable a. If the table fires no errors such as attempt to index field ‘?’ (a nil value) then pcall returns true else it returns false
It`s a bit messy but seems to work so far. Does anyone have any tips on working with pcall i.e. is it bad practice to use like this to avoid errors?
Just found this is great for housekeeping you might want to do with each reload of a tool in the global space! i.e. wont break the tool on firing up renoise but doesnt need to be linked to document create either:
--check if the song exists
if pcall(function() local a = renoise.song() end) then
-- reset global vals associated with song / tidy up changes made in renoise
end