Skip to content

Instantly share code, notes, and snippets.

@rozeappletree
Created February 7, 2020 13:16
Show Gist options
  • Save rozeappletree/43053664d84598433bbbd04fb7c43eeb to your computer and use it in GitHub Desktop.
Save rozeappletree/43053664d84598433bbbd04fb7c43eeb to your computer and use it in GitHub Desktop.
local t = {employees=
{employee= {
{
id= "1",
firstName= "Tom",
lastName= "Cruise",
["photo"]= "https=//jsonformatter.org/img/tom-cruise.jpg"
},
{
id= "2",
firstName= "Maria",
lastName= "Sharapova",
["photo"]= "https=//jsonformatter.org/img/Maria-Sharapova.jpg"
},
{
id= "3",
firstName= "Robert",
lastName= "Downey Jr.",
["photo"]= "https=//jsonformatter.org/img/Robert-Downey-Jr.jpg"
}
}
}
}
function containsKey(t, key, value)
--print("containsKey", key, value)
for _, k in ipairs(t) do
if k == key then
print(k, "matches", key)
return true
end
end
end
function containsValue(t, key, value)
--print("containsValue", key, value)
for _, v in ipairs(t) do
if v == value then
--print(k, "matches", value)
return true
end
end
end
function printTable(t1, tab, skipValues, callback)
if not skipValues then
skipValues = {}
end
if tab then
tab = tab .. "\t"
else
tab = ""
end
for k, v in pairs(t1) do
if callback(skipValues, k) then
--print(tab, k, "= skipped")
return v
else
if type(v) == "table" then
--print(tab, k, "{")
rv = printTable(v, tab, skipValues, callback)
if rv then
return rv
end
--print(tab, "}")
else
if callback(skipValues, k, v) then
--print(tab, k, "= skipped")
return v
else
--print(tab, k, "=", v)
end
end
end
end
end
rv = printTable(t, nil, {"employee"}, containsKey)
if type(rv) == "table" then
rv = printTable(rv, nil, {"Cruise"}, containsValue)
end
print("final:", rv)
local t = {employees=
{employee= {
{
id= "1",
firstName= "Tom",
lastName= "Tom Cruise",
["photo"]= "https=//jsonformatter.org/img/tom-cruise.jpg"
},
{
id= "2",
firstName= "Eve Maria",
lastName= "Sharapova",
["photo"]= "https=//jsonformatter.org/img/Maria-Sharapova.jpg"
},
{
id= "3",
firstName= "Robert",
lastName= "Downey Jr.",
["photo"]= "https=//jsonformatter.org/img/Robert-Downey-Jr.jpg"
}
}
}
}
function containsKey(t, key, value)
--print("containsKey", key, value)
for _, k in ipairs(t) do
if k == key then
print(k, "matches", key)
return true
end
end
end
function containsValue(t, key, value)
--print("containsValue", key, value)
for _, v in ipairs(t) do
if value and string.match(value, v) then
--print(k, "matches", value)
return true
end
end
end
function printTable(t1, tab, skipValues, callback)
if not skipValues then
skipValues = {}
end
if tab then
tab = tab .. "\t"
else
tab = ""
end
for k, v in pairs(t1) do
if callback(skipValues, k) then
--print(tab, k, "= skipped")
return v
else
if type(v) == "table" then
--print(tab, k, "{")
rv = printTable(v, tab, skipValues, callback)
if rv then
return rv
end
--print(tab, "}")
else
if callback(skipValues, k, v) then
--print(tab, k, "= skipped")
return v
else
--print(tab, k, "=", v)
end
end
end
end
end
rv = printTable(t, nil, {"employee"}, containsKey)
if type(rv) == "table" then
rv = printTable(rv, nil, {"Maria"}, containsValue)
end
print("final:", rv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment