How can I set variables by referencing their names in a string?

What I really want is self-modifying code in Lua, but since Apple doesn’t allow it (i.e. Lua’s DoString() function), and Corona doesn’t support it, is there any way I can load variables dynamically?

The require function does this only at start-up.

Here is what I’d like to do is something like this fictitious setVariable function which would be referenced as follows:

myVariables={“id”,“firstName”, “lastName”}

myValues={44,“Bob”, “Gordon”}

local a

for a=1, #myVariables do

   setVariable(myVariables[a],myValues[a])

end

Ideally, this code would effectively set id=44, firstName=“Bob” and lastName=“Gordon”

Any ideas? Is there a Lua function that Corona supports that would allow me to do this?

To be specific about my practical need, I am losing my textures on Android on ApplicationSuspend or ApplicationExit. Therefore, on ApplicationResume, I want to be able to load them up from a table, including their X and Y positions, too. Without the ability to setVariable as described above, I would need to write laborious code specific to every texture and location.

Perhaps use an index table?

myVariable["id"] = 44 myVariable["firstName"] = "Bob" 

You can create a setVariable function to create/modify/delete these easily.

–john

Hi,

Perhaps I am not 100% clear on your use case, but why not use hash tables?

[lua]
local myData = 

{

  { id=44, firstName=“Bob”, lastName=“Gordon” },

  { id=231, firstName=“Chris”, lastName=“Byerley”}

}

for entry in ipairs( myData ) do

 local entry = entry

 print( entry.id, entry.firstName, entry.lastName )

end

–== 44, “Bob”, “Gordon”

–== 231, “Chris”, “Byerley”
[/lua]

If you are interested in interacting with your app in Lua after publishing take a look at the free http://coronium.io platform for Corona.

Cheers.

Perhaps use an index table?

myVariable["id"] = 44 myVariable["firstName"] = "Bob" 

You can create a setVariable function to create/modify/delete these easily.

–john

Hi,

Perhaps I am not 100% clear on your use case, but why not use hash tables?

[lua]
local myData = 

{

  { id=44, firstName=“Bob”, lastName=“Gordon” },

  { id=231, firstName=“Chris”, lastName=“Byerley”}

}

for entry in ipairs( myData ) do

 local entry = entry

 print( entry.id, entry.firstName, entry.lastName )

end

–== 44, “Bob”, “Gordon”

–== 231, “Chris”, “Byerley”
[/lua]

If you are interested in interacting with your app in Lua after publishing take a look at the free http://coronium.io platform for Corona.

Cheers.