How to store a table position as a string? [Solved]

Post Reply
User avatar
v-rob
Developer
Posts: 970
Joined: Thu Mar 24, 2016 03:19
GitHub: v-rob
IRC: v-rob
Location: Right behind you.

How to store a table position as a string? [Solved]

by v-rob » Post

I have a table in Lua. Obviously, to access a something in a table, I have to do something like this:

Code: Select all

foo = {
    bar = {
        baz = "some text"
    }
}

print(foo.bar.baz)    -- prints "some text"
What I have is this stored as a string, like this:

Code: Select all

path = "foo.bar.baz"
I need to store it as a string because it has to be stored in node metadata. So my question is, how do I access this position in the table using the string? My current method uses loadstring to get it, but that's really messy. Is there any other way to do it?
Last edited by v-rob on Thu Oct 11, 2018 05:10, edited 1 time in total.
Core Developer | My Best Mods: Bridger - Slats - Stained Glass

isaiah658
Member
Posts: 168
Joined: Sun Jan 24, 2016 14:58
Contact:

Re: How to store a table position as a string?

by isaiah658 » Post

I believe what you are looking for is minetest.serialize and minetest.deserialize. https://dev.minetest.net/minetest.serialize

User avatar
v-rob
Developer
Posts: 970
Joined: Thu Mar 24, 2016 03:19
GitHub: v-rob
IRC: v-rob
Location: Right behind you.

Re: How to store a table position as a string?

by v-rob » Post

isaiah658 wrote:I believe what you are looking for is minetest.serialize and minetest.deserialize. https://dev.minetest.net/minetest.serialize
No, because I'm trying to store a position in a table, not store the table itself.
Core Developer | My Best Mods: Bridger - Slats - Stained Glass

User avatar
12Me21
Member
Posts: 873
Joined: Tue Mar 05, 2013 00:36
GitHub: 12Me21
Location: (Ignore all of my posts before 2018)

Re: How to store a table position as a string?

by 12Me21 » Post

What are you using this to do? There's probably a better way.

User avatar
GreenXenith
Member
Posts: 1356
Joined: Wed Oct 28, 2015 01:26
GitHub: GreenXenith
Location: UTC-8:00
Contact:

Re: How to store a table position as a string?

by GreenXenith » Post

Like 12Me12, I am uncertain this is the best way to accomplish whatever it is you are doing. However, I will take a stab at it.

Assuming you must use a string formatted as "foo.bar.baz" to access table position foo.bar.baz, you could go about it with something like this:

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"
The val splits the string by "." into it's components, and data uses those to find the desired position in the table.
YouTube | Mods | Patreon | Minetest Discord @greenxenith

You should not be able to read this message.

User avatar
12Me21
Member
Posts: 873
Joined: Tue Mar 05, 2013 00:36
GitHub: 12Me21
Location: (Ignore all of my posts before 2018)

Re: How to store a table position as a string?

by 12Me21 » Post

To allow any path length:

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.
Last edited by 12Me21 on Thu Oct 11, 2018 00:54, edited 1 time in total.

isaiah658
Member
Posts: 168
Joined: Sun Jan 24, 2016 14:58
Contact:

Re: How to store a table position as a string?

by isaiah658 » Post

Sorry I misunderstood what you meant. I'm not quite sure what you mean. I think something that might be helpful is to know that you can access tables either by foo.bar or by foo[bar] or foo["bar"]. The last method would allow you to access a table using a string rather than dot notation. Keep in mind that foo[bar] and foo["bar"] are different. The first would use the bar variable's value. The second would be a string. You can access nested tables with strings such as foo["bar"]["something"]["another_layer_deeper"].

User avatar
v-rob
Developer
Posts: 970
Joined: Thu Mar 24, 2016 03:19
GitHub: v-rob
IRC: v-rob
Location: Right behind you.

Re: How to store a table position as a string?

by v-rob » Post

Unfortunately, I think it would have to use a string because I'm getting this value with an input field in a formspec, so it would come in string form. And yes, I meant to put foo as a value in another table.

Anyway, I'll try 12Me21's solution for now and see if it works.

Edit: Yup, 12Me21's solution works perfectly. I adapted it a bit, and it works soooo much better than loadstring.
Core Developer | My Best Mods: Bridger - Slats - Stained Glass

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests