How to access value of local variable by using using its name (stored as string)? Is it even possible?

Hi, i am new to programming. My whole experience is one half-finished game written in very simple procedure language, so corona and lua is quite big step up. Sorry if i am asking totally obvius thing.

Accessing value of variable via name stored in a string:

It works with globals:

testVar = 5

local string = “testVar”

print(_G[string]) – 5

Or elements of local arrays (assuming i know which array contains it):

local testArr = {}

testArr.testVar = 5

local string = “testVar”

print(testArr[string]) --5

But how to get value of testVar in this case?

local testVar = 5

local string = “testVar”

???

Basically, i used textfield object to create “command line” at the bottom of game screen to request printing certain values from running program into console. Initially i wanted to use loadstring function for this purpose, but it seems there is no way (or good way at least) to make it work in any scope other than global. Therefore, loadstring doesnt work for printing such local variables either.

For me such feature looks like absolutely necessary thing for future debugging so any working solution will do. If its impossible then obly workaround i see is to use global variables for storing anything that needs to be kept indefinitely or for prolonged period of time (so it can be printed or overwritten “manually” if need arises).

Hey!

You can access table entries by their key, i.e.

local testArr = {} testArr.testVar = 5 local string = "testVar" print( testArr["string"] ) -- If you use square brackets, then you need the entry in "quotes". print( testArr.string ) -- You can also access them like this.

Also, you should be careful with using variable names like stringbecause string it overwrites an existing library.