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:
Normally i use this code: Lua Code:
So I write this function: Lua Code:
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:
Will very appreciate for help, or idea how I can fix this problem. |
Building tables programmatically isn't going to save you anything, IMO. You can do the same thing you were doing with the if checks, but in a more terse manner:
Code:
ADVANCED_INTERFACE_GLOBAL_CONSTANTS = ADVANCED_INTERFACE_GLOBAL_CONSTANTS or {} EDIT: I meant "isn't going to save you anything in this case" - I've built config tables programmatically in some of my AddOns, because otherwise I'd have a bunch of copy-paste code with slightly different labels. |
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) |
Quote:
Quote:
Regarding to last code example, I was not able to use it, because it's not checking existing tables. For example, I already got a table with some values, I don't want to touch them or delete, instead, I would like to add some new tables in it: https://repl.it/BSKi/1 Lua Code:
|
If this is for checking if your saved variables already exist because you're worried about overwriting them, that's not something you need to do.
Your saved variables are loaded after your addon files are executed and will overwrite any variables with the same name. If you have "ADVANCED_INTERFACE_GLOBAL_CONSTANTS = {}" in your lua file, and "ADVANCED_INTERFACE_GLOBAL_CONSTANTS" is a saved variable name, it'll just replace your empty table with the saved data when it finishes loading. |
This will probably work better.
Lua Code:
Unlike Barjack's function, this keeps existing entries and allows numeric and object keys. It's still much less efficient than Torhal's code. |
|
Quote:
Quote:
Quote:
|
All times are GMT -6. The time now is 04:03 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI