Created
January 31, 2026 07:39
-
-
Save 0riginaln0/b34f8ef2c2d98aa3b12aec5642ff7fca to your computer and use it in GitHub Desktop.
Get values from deeply nested tables
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
| x = { | |
| a = { | |
| b = { | |
| c = ";)" | |
| } | |
| } | |
| } | |
| print(x.a.b.c) | |
| function get_in(tbl, keys) | |
| local current = tbl | |
| for _, key in ipairs(keys) do | |
| if type(current) == "table" then | |
| current = current[key] | |
| else | |
| return nil | |
| end | |
| end | |
| return current | |
| end | |
| print(get_in(x, {"a", "b", "c"})) | |
| function get_in_(t, ...) | |
| for _, k in ipairs({...}) do | |
| t = type(t) == "table" and t[k] or nil | |
| end | |
| return t | |
| end | |
| print(get_in_(x, "a", "b", "c")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment