concatenate string to access table index

I’m retrieving field names from a database and trying to access a lua table index containing the field name. My code is:

for row in globalData.db:nrows([[SELECT bonusValue, name FROM buildingsBonuses WHERE buildingId = "]]..v[1]..[[" AND id = resourceId]]) do   print(inspect(row.name))   local test = row.name   print(inspect(globalData.userResources[test][bonus]))   -- globalData.userResources[row.name]bonus = globalData.userResources[row.name]bonus + row.bonusValue end

Would return:

"cash" 0 nil "happiness" 0 nil

And crash if I un-comment the last line.

How can I address this? Any alternative way to access a table when retrieve the indexes from a database (and getting as a string). I basically want to access  globalData.userResources.cash.bonus and  globalData.userResources.happiness.bonus

I also tried  local test = row.name…‘bonus’ but same result

afaik you cannot construct variable names like that

and where is the [bonus] value supposed to be taken from?

[quote name=“anaqim” post=“395188” timestamp=“1550000169”]afaik you cannot construct variable names like that and where is the [bonus] value supposed to be taken from?[/quote] globalData.userResources.cash.bonus (or similar) already exist from other piece of code. I just to update the value. Is there another I coulld come around this?

i am not a pro in this so i may very well be wrong, but i believe that when you assign a variable to something it refers to a specific memory address.

that variable cannot be artificially constructed by combining strings or numbers cause even if it looks the same, it is not.

try this and you’ll see:

local part="leg" local animal="horse" local animalpart=animal..part print(part) = "leg" print(animal) = "horse" print(animalpart) = "horseleg" print("animal".."part") = "animalpart" and not "horseleg"

to solve your issue means you need to reconstruct your operations and logic so it fits within the constraints of the lua language.

i cannot tell you how to do that since i dont know all your code, neither would i take the time to do that since its core functionailty that you absolutely need to figure out and understand yourself.

looks like a simple syntax error, but you don’t provide enough code for us to know for sure, fe:

-- code from the uncommented line makes it look like "bonus" is a variable containing the table key globalData.userResources[test][bonus] -- so you'd index with row.name as globalData.userResources[row.name][bonus] -- BUT code from the commented line makes it look like "bonus" might itself be the intended table key globalData.userResources[test]bonus -- so, if "bonus" is itself a table key rather than a variable, then you'd need a period in there: globalData.userResources[row.name].bonus -- equivalent to globalData.userResources[row.name]["bonus"]

so YOU will have to look at your table, figure out what “bonus” is and how to index with it, but either way the source of your problem seems to be a simple syntax error (missing punctuation)
 

[EDITED, formatting messed up during posting]

Thanks Dave, you put me on the right track. It actually work with the period before bonus:

globalData.userResources[row.name].bonus

afaik you cannot construct variable names like that

and where is the [bonus] value supposed to be taken from?

[quote name=“anaqim” post=“395188” timestamp=“1550000169”]afaik you cannot construct variable names like that and where is the [bonus] value supposed to be taken from?[/quote] globalData.userResources.cash.bonus (or similar) already exist from other piece of code. I just to update the value. Is there another I coulld come around this?

i am not a pro in this so i may very well be wrong, but i believe that when you assign a variable to something it refers to a specific memory address.

that variable cannot be artificially constructed by combining strings or numbers cause even if it looks the same, it is not.

try this and you’ll see:

local part="leg" local animal="horse" local animalpart=animal..part print(part) = "leg" print(animal) = "horse" print(animalpart) = "horseleg" print("animal".."part") = "animalpart" and not "horseleg"

to solve your issue means you need to reconstruct your operations and logic so it fits within the constraints of the lua language.

i cannot tell you how to do that since i dont know all your code, neither would i take the time to do that since its core functionailty that you absolutely need to figure out and understand yourself.

looks like a simple syntax error, but you don’t provide enough code for us to know for sure, fe:

-- code from the uncommented line makes it look like "bonus" is a variable containing the table key globalData.userResources[test][bonus] -- so you'd index with row.name as globalData.userResources[row.name][bonus] -- BUT code from the commented line makes it look like "bonus" might itself be the intended table key globalData.userResources[test]bonus -- so, if "bonus" is itself a table key rather than a variable, then you'd need a period in there: globalData.userResources[row.name].bonus -- equivalent to globalData.userResources[row.name]["bonus"]

so YOU will have to look at your table, figure out what “bonus” is and how to index with it, but either way the source of your problem seems to be a simple syntax error (missing punctuation)
 

[EDITED, formatting messed up during posting]

Thanks Dave, you put me on the right track. It actually work with the period before bonus:

globalData.userResources[row.name].bonus