Code: Select all
foo = {
bar = {
baz = "some text"
}
}
print(foo.bar.baz) -- prints "some text"
Code: Select all
path = "foo.bar.baz"
Code: Select all
foo = {
bar = {
baz = "some text"
}
}
print(foo.bar.baz) -- prints "some text"
Code: Select all
path = "foo.bar.baz"
No, because I'm trying to store a position in a table, not store the table itself.isaiah658 wrote:I believe what you are looking for is minetest.serialize and minetest.deserialize. https://dev.minetest.net/minetest.serialize
Code: Select all
local str = "foo.bar.baz"
--Considering your string has foo in it, I would assume foo and other keys are within a larger table.. otherwise the foo would be useless
local tbl = {
foo = {
bar = {
baz = "some text"
}
}
}
local val = str:split(".")
local data = tbl.val[1].val[2].val[3]
print(data) --prints "some text"
Code: Select all
function get(table, path)
for key in path:gmatch("[^%.]+") do
if type(table) ~= "table" then
return
end
table = table[key]
end
return table
end
local table = {
foo = {
bar = {
baz = "some text",
a = "aaaaaaa"
}
}
}
print(get(table, "foo.bar.baz"))
print(get(table, "foo.bar.a"))
print(get(table, "bad")) -- Note: this returns nil
print(get(table, "bad.bad")) -- ... but this doesn't return anything
-- Because `table.bad` is `nil`, while `table.bad.bad` would throw an error.
Users browsing this forum: Gmr_Leon and 2 guests