I would like to check if this structure is exists and if it is - do nothing, else build this structure:
Lua Code:
ADVANCED_INTERFACE_GLOBAL_CONSTANTS = {} -- root
ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS = {} -- child of root
ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE = {} -- child of child of root
ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION = {} -- child of child of child of root
Normally i use this code:
Lua Code:
if not ADVANCED_INTERFACE_GLOBAL_CONSTANTS then
ADVANCED_INTERFACE_GLOBAL_CONSTANTS = {}
end
if not ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS then
ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS = {}
end
if not ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE then
ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE = {}
end
if not ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION then
ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION = nil
end
But it's very long, and looks like "hardcodeing"
So I write this function:
Lua Code:
function test(table)
local orig
-- Lets split (table) string in to 4 strings: ADVANCED_INTERFACE_GLOBAL_CONSTANTS, STAMPS, ADVANCED_USER_INTERFACE, VERSION
for token in string.gmatch(table, '([^.]+)') do
-- Then if first string is root (ADVANCED_INTERFACE_GLOBAL_CONSTANTS) we do nothing
if orig == nil then
orig = token
else
-- Else, if it's not (ADVANCED_INTERFACE_GLOBAL_CONSTANTS) we concatenate strings in to (ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS), (ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE) and (ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION)
orig = orig.."."..token
end
-- Here we check if table exists - then do nothing - else we build our table or table in table...
if not _G[token] or not _G[orig] then
-- PS. token always == ADVANCED_INTERFACE_GLOBAL_CONSTANTS
print(orig)
--OUTPUT:
--FIRST RUN: ADVANCED_INTERFACE_GLOBAL_CONSTANTS
--SECOND RUN: ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS
--THIRD RUN: ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE
--LAST RUN: ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION
_G[orig] = {}
print(_G[orig])
--OUTPUT:
--FIRST RUN: table: 0x24425d0
--SECOND RUN: table: 0x24426c0
--THIRD RUN: table: 0x2442680
--LAST RUN: table: 0x2442860
end
end
end
test("ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION")
-- TESTING OUR test FUNCTION --
print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS)
--OUTPUT: table: 0x1e8ab20 (Correct table)
print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS)
--OUTPUT: nil (that mean table with STAMPS not exists :( )
print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE)
--OUTPUT: error: input: attempt to index a nil value (field 'STAMPS')
print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION)
--OUTPUT: error: input: attempt to index a nil value (field 'STAMPS')
Link to check this code:
http://codepad.org/wGpAa1ph
The problem is that this function build strange tables and i can't print them or use them. Looks like
if orig = ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS (with dot), then
_G[orig] = {} not woring (tables are build inside function, but outside, it's broken)
Lua Code:
print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS)
--OUTPUT: nil instead of nil. must be table
print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE)
--OUTPUT: error: input: attempt to index a nil value (field 'STAMPS')
print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION)
--OUTPUT: error: input: attempt to index a nil value (field 'STAMPS')
Will very appreciate for help, or idea how I can fix this problem.