View Single Post
10-20-15, 07:58 AM   #3
Barjack
A Black Drake
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 89
The problem with your code is that you can't use _G to look for tables in tables like that. For example, _G["one.two.three"] is looking for a single key in the global table with the value "one.two.three", it's not looking for table "one" then finding its key "two" then finding its key "three" etc., like it would if you wrote one.two.three in code.

Whether this is a good idea or not I don't know, but you can fix your code by avoiding using the global object like that. Here's a slightly different and working example:

Code:
function generate_subtables(str)
	local base = {}
	local current = base
	for token in string.gmatch(str, '([^.]+)') do
	    local t = {}
	    current[token] = t
	    current = t
	    print(token)
	end
	return base
end

ADVANCED_INTERFACE_GLOBAL_CONSTANTS = generate_subtables("STAMPS.ADVANCED_USER_INTERFACE.VERSION")
print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS)
print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS)
print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE)
print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION)
You can evaluate it here: https://repl.it/BSKi
  Reply With Quote