Created
February 7, 2020 13:16
-
-
Save rozeappletree/43053664d84598433bbbd04fb7c43eeb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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