lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


1) Actually, lua_getglobal does not do . resolution.

2) However, there is an undocumented interface in lauxlib, luaL_findtable(). This doesn't actually do what you want either, but it shows how you might write such a function in c.

In Lua, it's pretty easy:

function getdotted(field, start)
  start = start or _G
  for k in field:gmatch"[^.]+" do
    start = start[k]
    if start == nil then break end
  end
  return start
end

3) Please figure out how to turn off HTML mail for list submissions

4) Sorry for top-posting


On 1-Dec-06, at 12:27 PM, wang xu wrote:

Jim,

Thanks! It works.

I also tried to change the value:
> =loadstring("return "..path_to_n2)()
 hello
 > =loadstring(path_to_n2.."='world'")()
 > =n1.n2
 world
 >

However, I'm not feeling comfortable with it..

To make it looks a little bit nicer..Maybe I need to wrap lua_getglobal/lua_setglobal in C, and then I can use:

getglobal(path_to_n2)
setglobal(path_to_n2, "a new value for n1.n2")

Or, is it meaningful for Lua to add a dynamic resolving operator?

2006/12/2, Jim Whitehead II <jnwhiteh@gmail.com>:
 You can use loadstring, i.e.

 Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
 > t = {a = true}
 > =loadstring("return t.a")()
 true
 >

 In your case something like this should work:

 loadstring("return " .. path_to_n2)()


On 12/1/06, wang xu <xu4wang@gmail.com> wrote:


If I have a string containing the path to a varialble, for example:

> n1={n2="hello"}
> =n1.n2
hello
> path_to_n2="n1.n2"
> print(path_to_n2)
n1.n2
>

Given variable path_to_n2, how can I get the value of n1.n2 ?