Converting variable name to a string

Hello -
What would you recommend be the best way to do the following:

I have a requirement of using an existing variables' name as a string in the Lua code.

Currently, I am forced to have two tables with as such:

1
2
3
4
5
6
7
8
9
10
11
12
table1 = { variable1, variable2, variable3 }
table2 = { "variable1", "variable2", "variable3" }
 
Is there a way to combine the two so I don't have the above duplication? I realize that the second table contains a list of strings whereas the first contains a list of variables.
 
However, both have the same name, as you can see.
 
Is there some way in Lua to just have one table (In this case, table1) and then convert the variable name to a string as necessary?
 
I have tried the "tostring" function but wasn't successful with it.
 
Thank you.

Updated entry (Tried modifying the original post but unable to do so. Here it is again with the correct Lua code formatting):

Hello -
What would you recommend be the best way to do the following:

I have a requirement of using an existing variables' name as a string in the Lua code.

Currently, I am forced to have two tables with as such:

1
2
table1 = { variable1, variable2, variable3 }
table2 = { "variable1", "variable2", "variable3" }

this might help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
table1 = {}
 
table1["myVar"]=999
print(table1.myVar) -- 999
 
table1.myVar=123
print(table1["myVar"]) -- 123
 
local varString="myVar"
table1[varString]=456
print(table1.myVar) -- 456
 
table1.myVar = 789
print(table1["my".."Var"]) -- 789

Thank you very much, jmp909!

Hi jmp909 -

Here is some more additional info as to what I am trying to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
text1 = "Some Variable"
text2 = "Some Other Variable"
text3 = "Yet Another Variable"
d = {}
 
local textTable1 = { text1, text2, text3 } -- Table of variables
local textTable2 = { "text1", "text2", "text3" } -- Table of labels for above variables
 
-- This is the listener for the display Text object
local function textTouch(event)
   local id = event.target.id
   if(event.phase == "began") then
   transition.to( text[id]:setTextColor(255,255,55), { time=50, alpha=1.0})
   d = text[id] -- As a test, this should save the variable (Not text label)
   elseif(event.phase == "moved") then
   transition.to( text[id]:setTextColor(255,255,55), { time=50, alpha=1.0 })
   elseif(event.phase == "ended") then
   transition.to( text[id]:setTextColor(255,255,255), { time=100, alpha=1.0 })
   end
end
 
-- Create text display objects that have listeners for touch events
text = {}
for k, v in ipairs(textTable2) do
        local myText = display.newText( v, m, n, "Helvetica", 16 )
        myText:setReferencePoint(display.TopLeftReferencePoint)
        myText.id = k
        myText:setTextColor(255,255,255)
        myText:addEventListener("touch", textTouch)
        text[k] = myText
end

but you never use textTable1 in your code after defining it

Sorry - You're right.

Please replace this line:

1
d = text[id]

yes you are :) what are you actually trying to do?

What I am trying to do is:

- Create 3 text display objects.
- Assign touch events to these 3 text display objects.
- When a text object receives a touch event, it sends its name which is also the name of a variable to a function for processing some action.

For example:
I have these 3 text objects.
- Bird
- Cat
- Dog

Bird, Cat, and Dog are also variables.

1
bird = "Say Hello"; cat = "Meow"; dog = "Woof"

at the risk of confusing you ;) ....

main.lua

1
2
3
4
5
6
7
8
9
10
11
12
local Word = require("Word")
 
words = {}
 
words[1] = Word.new("Dog", "Woof", 100,100)
words[2] = Word.new("Cat", "Meow", 150,100)
words[3] = Word.new("Bird", "Tweet", 200,100)
 
words[1]:speak()
 
print(words[1].hasSpoken) -- true
print(words[2].hasSpoken) -- false

Wow - Awesome.
Thank you!

The Force is strong with you, Master Jedi.

And just as a final comment - Any recommendations on how to someday get to where you are? :)

i.e. Studying the code you provided is one way - Thanks!

Do you have any other recommendations? Just trial and error? I've been reading the programming Lua book, or attempting to.

I see this will take many years to get good - But a very nice journey!

program flash for 7 years. spend a few weeks learning Lua/Corona. read the docs & forums. trial and error.... even making that example for you i got it wrong 3 times. there's modules and there's metatables, then there's mixing the two like i've done, and i've still not got the difference totally nailed yet. I do know that metatables save memory because you're not adding the functions to every instance of your object. just the master that they are derived from.

i still haven't standardized my coding practices yet. i just make something that works then improve it later. but largely based on what other people are doing too. download all the the code samples like Beebe games' stuff. and open up all the code in the Corona samples and see how they do it.

j

That's a pretty clear and simple example of how to use metatables, thanks. If I ever need to need to save memory this is a good example to refer to; whether or not to use metatables appears to be a tradeoff between memory and execution speed:
http://developer.anscamobile.com/forum/2011/01/21/oop-game-classes-objects-inheritance-methods-properties

This will help me alot....Thanks jmp909

views:2029 update:2011/10/5 21:23:48
corona forums © 2003-2011