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

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

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

by v-rob » Wed Oct 10, 2018 21:49

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.
 

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

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

by isaiah658 » Wed Oct 10, 2018 22:05

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

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

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

by v-rob » Wed Oct 10, 2018 23:02

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.
 

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

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

by 12Me21 » Wed Oct 10, 2018 23:08

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

User avatar
GreenDimond
Member
 
Posts: 1272
Joined: Wed Oct 28, 2015 01:26
Location: SCP-3008
GitHub: GreenXenith
IRC: GreenDimond
In-game: GreenDimond

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

by GreenDimond » Thu Oct 11, 2018 00:42

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.
Y▹uTube | Mods | Patre●n | Twitter | Minetest Discord | GreenDimond#3232

Hey, you. You're finally awake.
You were trying to view their profile, right? Found the rest of their signature, same as us, and that guest over there.
 

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

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

by 12Me21 » Thu Oct 11, 2018 00:51

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: 36
Joined: Sun Jan 24, 2016 14:58

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

by isaiah658 » Thu Oct 11, 2018 00:53

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
Member
 
Posts: 685
Joined: Thu Mar 24, 2016 03:19
Location: Right behind you.
GitHub: v-rob

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

by v-rob » Thu Oct 11, 2018 01:43

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.
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 4 guests