SNIPPET : getDictionaryPropertyTable

Go down

SNIPPET : getDictionaryPropertyTable Empty SNIPPET : getDictionaryPropertyTable

Post by Regg on Fri 08 Mar 2013, 13:28

Code:
--[[

Example Dictionary:

dictionaryName = {
    ["item1"] = {
        Property1 = "item1Property1",
        Property2 = "item1Property2",
        Property3 = "item1Property3",
        },
    ["item2"] = {
        Property1 = "item2Property1",
        Property2 = "item2Property2",
        Property3 = "item2Property3",
        },
    ["item3"] = {
        Property1 = "item3Property1",
        Property2 = "item3Property2",
        Property3 = "item3Property3",
        },
    };

getDictionaryPropertyTable(dictionaryName, "Property2") will return
{"item1" = "item1Property2", "item2" = "item2Property2", "item3" = "item3Property2"}

]]--
function getDictionaryPropertyTable(dictionaryName, propertyName)
    if type(dictionaryName) ~= "table" then
        return false;
    end
    local count = 0
    local results = {}
    for index, item in pairs(dictionaryName) do
        if isIndexInTable(propertyName, item) then
            results[index] = item[propertyName]
            count = count + 1
        end
    end
    if count > 0 then
        return results
    end
    return false
end

function isIndexInTable(entity, tableName)
    if entity == nil or tableName == nil then
        return false;
    end
    for i, v in pairs(tableName) do
        if type(i) == "string" and string.lower(entity) == string.lower(i) then
            return true
        elseif type(i) == "number" and entity == i then
            return true
        end
    end
    return false
end
Regg
Regg
Veteran

Points : 143
Reputation : 10
Join date : 2013-02-22

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum