lua-users home
lua-l archive

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


On Tue, 5 Feb 2002, Markus Huber wrote:

> Example:
> 
> Lol={}
> Lol.Write={}
> Lol.Write.Version=001
> print(Lol.Write.Version) --> print expected 1
> Name='Write'
> print(getglobal('Lol.'..Name..'.Version')) --> print nil ???
> 
> Needed for Robertos Ierusalimschy library suggestion (simple example):


For once I looked before sending this out as an a possible solution and
saw several other versions of the same thing. however, this one might be
closer to what you're looking for, so here it is anyway.

 function lookup(path)
	root = globals()
	local step = function(key)
		if type(root) == "table" then
			root = root[key]
		else
			root = nil
		end
	end
	gsub(path, "(%w+)", step);
	return root
 end

Then you can do:

 print(lookup("Lol.Write.Version"))

Which will print the expected 1 value. This version returns nil if the
path is undefined (for example, if Lol.Write is a string rather than a
table).

Note that the function depends on some changes in the way lexical scoping
works for 4.1work3. Still, you should be able to modify it for 4.0 for 4.1
alpha fairly easily.

   - Tom Wrensch