Thread Tools Display Modes
Prev Previous Post   Next Post Next
10-20-15, 06:50 AM   #1
Nikita S. Doroshenko
A Cyclonian
 
Nikita S. Doroshenko's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2015
Posts: 45
Lua Tables - How can I check if table in table in another table in... exists?

I would like to check if this structure is exists and if it is - do nothing, else build this structure:
Lua Code:
  1. ADVANCED_INTERFACE_GLOBAL_CONSTANTS = {} -- root
  2. ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS = {}  -- child of root
  3. ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE = {}  -- child of child of  root
  4. ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION = {}   -- child of child of child of root

Normally i use this code:
Lua Code:
  1. if not ADVANCED_INTERFACE_GLOBAL_CONSTANTS then
  2.         ADVANCED_INTERFACE_GLOBAL_CONSTANTS = {}
  3.     end
  4.     if not ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS then
  5.         ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS = {}
  6.     end
  7.     if not ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE then
  8.         ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE = {}
  9.     end
  10.     if not ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION then
  11.         ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION = nil
  12.     end
But it's very long, and looks like "hardcodeing"
So I write this function:

Lua Code:
  1. function test(table)
  2.     local orig
  3. -- Lets split (table) string in to 4 strings: ADVANCED_INTERFACE_GLOBAL_CONSTANTS, STAMPS, ADVANCED_USER_INTERFACE, VERSION
  4.     for token in string.gmatch(table, '([^.]+)') do
  5. -- Then if first string is root (ADVANCED_INTERFACE_GLOBAL_CONSTANTS) we do nothing
  6.         if orig == nil then
  7.             orig = token
  8.         else
  9. -- 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)
  10.             orig = orig.."."..token
  11.         end
  12. -- Here we check if table exists - then do nothing - else we build our table or table in table...
  13.         if not _G[token] or not _G[orig] then
  14. -- PS. token always == ADVANCED_INTERFACE_GLOBAL_CONSTANTS
  15.             print(orig)
  16. --OUTPUT:
  17. --FIRST RUN: ADVANCED_INTERFACE_GLOBAL_CONSTANTS
  18. --SECOND RUN: ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS
  19. --THIRD RUN: ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE
  20. --LAST RUN: ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION
  21.             _G[orig] = {}
  22.             print(_G[orig])
  23. --OUTPUT:
  24. --FIRST RUN: table: 0x24425d0
  25. --SECOND RUN: table: 0x24426c0
  26. --THIRD RUN: table: 0x2442680
  27. --LAST RUN: table: 0x2442860
  28.         end
  29.     end
  30. end
  31.  
  32. test("ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION")
  33. -- TESTING OUR test FUNCTION --
  34. print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS)
  35. --OUTPUT: table: 0x1e8ab20 (Correct table)
  36. print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS)
  37. --OUTPUT: nil (that mean table with STAMPS not exists :( )
  38. print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE)
  39. --OUTPUT: error: input: attempt to index a nil value (field 'STAMPS')
  40. print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION)
  41. --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:
  1. print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS)
  2. --OUTPUT: nil instead of nil. must be table
  3. print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE)
  4. --OUTPUT: error: input: attempt to index a nil value (field 'STAMPS')
  5. print(ADVANCED_INTERFACE_GLOBAL_CONSTANTS.STAMPS.ADVANCED_USER_INTERFACE.VERSION)
  6. --OUTPUT: error: input: attempt to index a nil value (field 'STAMPS')

Will very appreciate for help, or idea how I can fix this problem.

Last edited by Nikita S. Doroshenko : 10-20-15 at 06:53 AM.
  Reply With Quote
 

WoWInterface » Developer Discussions » Lua/XML Help » Lua Tables - How can I check if table in table in another table in... exists?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off