Curious if it’s possible to initialize an array of size zero. I’m modifying some arrays at runtime and am having a hard time removing an element when only 1 element remains in the array. I could do something like set the last element to a large negative number or something but am curious if this is possible before I go modifying all of my code to support something like that.
On init pass I noticed that some of the conditionally initialized arrays will exist so calling lenarray
on them doesn’t seem to throw any errors and simply returns 0. Am looking to do this within an UDO for removing an element so I can return an array of size 0 if there is only 1 element remaining in the array.
Thanks in advance as always
Cheers
Figured I’d post the solution here if anyone is interested, very hacky but hey, it works. I modified one of the UDO’s provided by @joachim from this thread. For the removal and returning of an empty array I just initialize a variable and then put the array initialization in a conditional that will never execute. If you pass in an empty array to the add element functionality it will check for that and then initialize a new array and insert the desired element as the only element in the array.
;Removes an index from from an array and returns new array
opcode arrayremoveindex, i[], i[]i
iInArr[], iIndx xin
iA init 1
if(lenarray(iInArr) == 1) then
if(iA == 0)then
iOutArr[] = iInArr
endif
else
iOutArr[] init lenarray(iInArr)-1
iReadIndx = 0
until iReadIndx == iIndx do
iOutArr[iReadIndx] = iInArr[iReadIndx]
iReadIndx += 1
enduntil
iReadIndx += 1
until iReadIndx == lenarray(iInArr) do
iOutArr[iReadIndx-1] = iInArr[iReadIndx]
iReadIndx += 1
enduntil
endif
xout iOutArr
endop
opcode arrayaddelement, i[], i[]ij
iInArr[], iEl, iPos xin
prints "Array length"
if(lenarray:i(iInArr) == -1) then
iOutArr[] init 1
iOutArr[0] = iEl
else
iOutArr[] init lenarray:i(iInArr)+1
iPos = (iPos == -1) ? lenarray:i(iInArr) : iPos
iWriteIndx = 0
iIndxDiff = 0
while iWriteIndx < lenarray:i(iOutArr) do
iReadIndx = iWriteIndx - iIndxDiff
if iWriteIndx == iPos then
iOutArr[iWriteIndx] = iEl
iIndxDiff = 1
else
iOutArr[iWriteIndx] = iInArr[iReadIndx]
endif
iWriteIndx += 1
od
endif
xout iOutArr
endop
2 Likes