String to access nested table value.
Posted: Mon Apr 19, 2021 3:17 pm
The question I have is more of a Lua specific question, so hopefully I am alright to ask here.
Lua has two main ways of accessing a key in a table that I know of.
This all works fine and is simple enough, but I am wanting to do a nested access with a single . seperated string.
I'm sure I could loop through and seperate each . seperated value into a table. Then loop that to access the value, but I was wondering if a more sane way of doing this existed.
Thanks for any advice.
Lua has two main ways of accessing a key in a table that I know of.
Code: Select all
local t = {x=0, y=5, other={z=10}}
-- Both should print "0"
print(t["x"])
print(t.x)
-- "x" can also be a variable
local prop = "x"
-- First prints "0". Second prints "nil"
print(t[prop])
print(t.prop)
Code: Select all
local t = {x=0, y=5, other={z=10}}
local zprop = "other.z"
-- This prints "nil"
print(t.zprop)
Thanks for any advice.