Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

concatenate string to access table index
Started by J@V0 Feb 12 2019 11:21 AM

- - - - -
5 replies to this topic
table; string; concatenate;

Best Answer davebollinger , 12 February 2019 - 12:00 PM

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]

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

J@V0

[GLOBAL: userInfoPane.html]
J@V0
  • Enthusiast

  • 43 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 762 posts
  • Corona SDK

afaik you cannot construct variable names like that

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



[TOPIC: post.html]
#3

J@V0

[GLOBAL: userInfoPane.html]
J@V0
  • Enthusiast

  • 43 posts
  • Corona SDK

afaik you cannot construct variable names like that
and where is the [bonus] value supposed to be taken from?


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?

[TOPIC: post.html]
#4

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 762 posts
  • Corona SDK

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.



[TOPIC: post.html]
#5

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,309 posts
  • Enterprise

  Best Answer

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]



[TOPIC: post.html]
#6

J@V0

[GLOBAL: userInfoPane.html]
J@V0
  • Enthusiast

  • 43 posts
  • Corona SDK

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

globalData.userResources[row.name].bonus




[topic_controls]
[/topic_controls]