[cmd] ListDistinct

stable
By Strooth in Miscellaneous Published December 2021 👁 1,098 views 💬 0 comments

Description

For use where ArrayDistinct() is not available.
HaasScript
-- Author: Strooth
DefineCommand('ListDistinct', 'Returns only unique values')
--local default = {['a']={1,2,3,4},'d','g',1,4,8,3,'3','4',{1}, ok="AverageOrderbookSpread",{this={'a','d','g',1,4,8,3,'3','4','m'}}}
local convert 
local list = DefineParameter(ListDynamicType, 'list', 'The list to convert', false, {}, 'ListDynamicType')
local logging = DefineParameter(BooleanType, 'logging', 'enable log', false, false, 'boolean, true/false')
local flat = {}

local logit = function(en, type, key, value)
    if en then 
        if type == 1 then 
            local keytype = Parse(GetType(key), StringType) 
            local valuetype = Parse(GetType(value), StringType)
            Log('keytype = '..keytype..' = '..key)
            Log('valuetype = '..valuetype..' = '..convert(value, false))
        elseif type == 2 then 
            Log(key)
            Log(GetType(value))
            Log(convert(value))
            Log(value)
        end
    end
end

convert = function(list, newlist)
    local count = 0
    local join = function(_list, _value)
            if count == 0 then 
                count = count + 1 
                if ArrayContains(_list, _value) then 
                    return _list
                else 
                    _list[#_list+1] = _value
                    return _list
                end
            else 
                if ArrayContains(_list, _value) then 
                    return _list
                else 
                    _list[#_list+1] = _value
                    return _list
                end
            end 
    end
    local string = ''
    if StringContains(GetType(list), ArrayDataType) then
        for key, value in pairs(list) do 
            logit(logging, 1, key, value)
            if StringContains(GetType(value), ArrayDataType) then 
                logit(logging, 2, key, value)
                newlist = convert(value, newlist)
            else
                newlist = join(newlist, value)
            end
        end
    end 
    return newlist
end
flat = convert(list, flat)
DefineOutput(ListDynamicType, flat, '', '')

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!