[pshaiCmd] ArrayFilter2

stable
By pshai in Miscellaneous Published August 2022 👁 1,127 views 💬 0 comments

Description

ArrayFilter command that works with numbers and text too. Example of use:

-- build test arrays
local array1 = {} -- will contain GUID values as text
local array2 = {} -- will contain numbers
 
for i = 1, 10 do
    array1[i] = NewGuid()
    array2[i] = i
end
 
-- filter array1
Log('---- Array1 ----')
Log(array1) -- log the unfiltered array

-- grab values for indices 1, 5, 3 and 7 
local id1 = array1[1]
local id2 = array1[5]
local id3 = array1[3]
local id4 = array1[7]
 
-- filter each value exclusively from the array 
array1 = CC_ArrayFilter2(array1, id1, ArrayFilterExclusiveType)
array1 = CC_ArrayFilter2(array1, id2, ArrayFilterExclusiveType)
array1 = CC_ArrayFilter2(array1, id3, ArrayFilterExclusiveType)
array1 = CC_ArrayFilter2(array1, id4, ArrayFilterExclusiveType)
 
Log('-----')
Log(array1) -- log the filtered array
Log('---------------------')
 
-- filter array2
Log('---- Array2 ----')
Log(array2) -- log the unfiltered array

-- grab values for indices 1, 5, 3 and 7
local id1 = array2[1]
local id2 = array2[5]
local id3 = array2[3]
local id4 = array2[7]

-- filter each value exclusively from the array 
array2 = CC_ArrayFilter2(array2, id1, ArrayFilterExclusiveType)
array2 = CC_ArrayFilter2(array2, id2, ArrayFilterExclusiveType)
array2 = CC_ArrayFilter2(array2, id3, ArrayFilterExclusiveType)
array2 = CC_ArrayFilter2(array2, id4, ArrayFilterExclusiveType)
 
Log('-----')
Log(array2) -- log the filtered array
Log('---------------------')
 
-- test error with unsupported filter type
array2 = CC_ArrayFilter2(array2, id1, ArrayFilterGreaterThanType)
HaasScript
-- [pshaiCmd] ArrayFilter2
-- Author: pshai

-- command definition
DefineCommand('ArrayFilter2', 'Array filtering that works with text too')

-- command inputs
local source = DefineParameter(ListDynamicType, 'source', 'Array consisting of numbers or text', true, {1, 2, 3})
local value = DefineParameter(DynamicType, 'value', 'Value to be included/excluded from the results', true, 2, 'Number, Text')
local type = DefineParameter(EnumType, 'type', 'Type of filter. Only ArrayFilterExclusiveType and ArrayFilterInclusiveType works! Optional, by default is ArrayFilterExclusiveType', false, ArrayFilterExclusiveType, 'ArrayFilterExclusiveType, ArrayFilterInclusiveType')

-- check for correct filter types
if type != ArrayFilterExclusiveType
and type != ArrayFilterInclusiveType
then
    LogError('CC_ArrayFilter2: Only ArrayFilterExclusiveType and ArrayFilterInclusiveType types work!')
    return
end

-- da magic
local function arrayFilter(array, value, type)
    local t = GetType(array)

    -- we only want to filter arrays and objects.
    -- otherwise, return the value as is.
    if t != ArrayDataType
    and t != UserDataDataType
    then
        return array
    end

    -- variables we need
    local result = {}
    local len = Count(array)

    -- loop through whole array
    for i = 1, len do
        local v = ArrayGet(array, i)

        -- check value according to filter type and add it to results
        if (type == ArrayFilterInclusiveType and v == value)
        or (type == ArrayFilterExclusiveType and v != value)
        then
            result = ArrayAdd(result, v)
        end
    end

    -- return filtered array
    return result
end

-- run the source array through the filtering function
source = arrayFilter(source, value, type)

-- return the filtered source
DefineOutput(ListDynamicType, source, 'Filtered results')

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!