Quick actions

cmd+k|ctrl+k

Navigation

Languages

GetKeysFromDict

Snippet info

Language

Lua

Visibility

public

Author

legacy-v1-16166

Created

2023-06-21T04:17:13.300615Z

Updated

2024-04-17T23:03:28.214928Z

local GetKeys = function(Dict)
    assert(type(Dict) == "table", "Expected 1st argument to be a dict")
    assert(Dict[1] == nil, "Expected 1st argument to be a dict")
    
    local Keys = {}
    
    for Key, _ in pairs(Dict) do
        table.insert(Keys, Key)
    end
    
    return Keys
end

local Dict = {A = "B", C = "D"}
local Keys = GetKeys(Dict) --> {A, C}

print(Keys)

local s, e = pcall(function()
    local Array = {"A", "B"}
    return GetKeys(Array)
end)

print(e) --> Error

local s2, e2 = pcall(function()
    local String = "A, B"
    return GetKeys(String)
end)

print(e2) --> Error
INFO