[pshaiCmd] ArrayRemove

stable
By pshai in Miscellaneous Published May 2020 👁 1,582 views 💬 0 comments

Description

Simple custom command to remove a cell from an array. Usage testample:
local testArr1 = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'}
local testArr2 = {1, 2, 3, 4, 5, 6, 7, 8, 9}

local result1 = CC_ArrayRemove(testArr1, 4)
local result2 = CC_ArrayRemove(testArr2, 4)
local result3 = CC_ArrayRemove(testArr1, 1)
local result4 = CC_ArrayRemove(testArr2, #testArr2)

Log(result1) -- missing 'd'
Log(result2) -- missing '4'
Log(result3) -- missing 'a'
Log(result4) -- missing '9'
HaasScript
DefineCommand('ArrayRemove', 'Removes a cell from array, pointed at with an index')

local input = DefineParameter(ListDynamicType, 'input', 'The array', true, ClosePrices())
local index = DefineParameter(NumberType, 'index', 'Index of the cell to be removed. Must be >0 (higher than zero).', true, 1)


if #input == 0 then
    LogError('CC_ArrayRemove: Array contains no cells!')
end

if index <= 0 then
    LogError('CC_ArrayRemove: Index must be higher than zero (>0).')
end

local temp1 = Grab(input, 0, index-1)
local temp2 = Grab(input, index, #input - index)
local result

if #temp1 == 0 then
    result = temp2
elseif #temp2 == 0 then
    result = temp1
else
    result = ArrayConcat(temp1, temp2)
end

DefineOutput(DynamicType, result, 'Array')

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!