Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua hierarchy string to table

Tags:

lua

lua-table

Is there a way that I can convert a hierarchy string into table form?

Suppose the input is A.B.C.D

ouput should be a table which traverses above input: A = {} A.B = {} A.B.C = {} A.B.C.D = {}

Thanks.

like image 614
Darshan Nair Avatar asked Jul 11 '13 05:07

Darshan Nair


2 Answers

The obvious solution would be to parse the string up and construct the hierarchy table from that. But a more clever solution is to let lua do it for you. With a bit of metamagic and function environment manipulation this can be done:

dump = require 'pl.pretty'.dump -- convenient table dumper from penlight

function createtable(str)
  local env_mt = {}
  env_mt.__index = function(t, k)
                     rawset(t, k, setmetatable({}, env_mt))
                     return rawget(t, k)
                   end
  local env = setmetatable({}, env_mt)
  local f = loadstring("return "..str)
  setfenv(f, env)
  f()
  return env
end

dump( createtable "A.B.C.D" )

this outputs:

{
  A = {
    B = {
      C = {
        D = {
        }
      }
    }
  }
}
like image 70
greatwolf Avatar answered Oct 17 '22 22:10

greatwolf


@greatwolf's answer is right but I prefer the more straightforward approach of "parsing" the string and constructing the table. Less magic, and you do not execute a function loaded from a (possibly) user-defined string, which would be a security issue.

local createtable = function(str)
  local top = {}
  local cur = top
  for i in str:gmatch("[^.]+") do
    cur[i] = {}
    cur = cur[i]
  end
  return top
end

(require "pl.pretty").dump(createtable("A.B.C.D"))
like image 45
catwell Avatar answered Oct 18 '22 00:10

catwell