get the name of a variable

I would like to get the name of a variable and display it as text. Does anyone know how to do this?

I tried tostring but it just turns the value of the variable to string not the name of the variable. [import]uid: 8192 topic_id: 3493 reply_id: 303493[/import]

n1 = {“hi”}
s1 = “hello”

If you are asking about how to get “n1” from the table reference to {“hi”} or “s1” from the string “hello”,
then you cannot… n1 and s1 are references to the table and string ‘objects’.

You could have multiple references, which shows why the question cannot be asked:

n2 = n1
s2 = s1

which variable name would you like to have?

[import]uid: 8093 topic_id: 3493 reply_id: 11157[/import]

Hi Frank I am actually trying to get “n1” from n1 I have to try it but maybe the tostring function.

I want to display the a text field that shows the name of a variable. [import]uid: 8192 topic_id: 3493 reply_id: 11158[/import]

Generally variables that refer to objects can have customer attributes, where as primitives cannot, so if your variables are not primitives, you can set custom attributes as so,

local Group = display.newGroup()  
Group.myName = "Group"  
  
.  
.  
  
object = event.Target  
print(object.myName)  
  
.  
.  

If this was a primitive, like a number of a string, you would get an error that states that you are trying to index it.

If using primitives is very important to you, then create tables for each variable that way you can tag it with whatever custom data you want. I am unsure of the overheads of this approach.

However, if anyone knows how to use referenceByString type of approach in Lua, what I am after is

theName = "Jayant C Varma"  
  
myVariable = "theName"  
print (referenceByString(myVariable)) --- should print Jayant C Varma  

and as in Objective-C where you can call a selector by name, so

myFunction = "moveLeft" callFunctionByName(myFunction)

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3493 reply_id: 11173[/import]

[lua]function v2s(var)
for k,v in pairs(_G) do
if v==var then
return k
end
end
end

myVar=77 --has to be global
print(v2s(myVar)) —> myVar

–Attention: if you assign myVar’s value to another var, you will now get that var’s name!
hisVar=myVar
print(v2s(myVar)) —> hisVar
print(v2s(hisVar)) —> hisVar
–Attention2: if you have an other global variable that has the same value as myVar you may get the other var’s name![/lua] [import]uid: 7356 topic_id: 3493 reply_id: 11232[/import]

That is great but it cannot help in creating the following

for i=1,10 do  
 theVar = "variable\_" .. i  
  
 callFunction(theVar) -\> callFunction(variable\_1)  
end  

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3493 reply_id: 11235[/import]

Don’t exactly get it… where is the print? Inside the function? It should be inside the loop.
You will take 10 same var names, this way.

What do you want to achieve? [import]uid: 7356 topic_id: 3493 reply_id: 11236[/import]

Hi Magenda,

let’s say the code is

  
variable\_1="Hello"  
variable\_2="world"  
variable\_3="really"  
variable\_4="cruel"  
variable\_5="cold"  
variable\_6="world"  
variable\_7="greetings"  
variable\_8="to"  
variable\_9="you"  
for i=1,9 do  
 print ("variable\_" .. i)  
end  

This should print the contents of the variable, I know that arrays are a better way to deal with this, but I just was curious to know if it can be done with variables. With referencing, UNIX shell scripts manage this very easily.

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3493 reply_id: 11237[/import]

I don’t know if this will help your situation, but all global variables are stored in a global table called _G.

And you can reference them like this:

_G[“yourvariablename”]

So if you had a global variable in your main.lua file, something like this:

score = 100

You could reference that from a different module, like this:

if _G[“score”] >= 50 then
do something
end [import]uid: 8444 topic_id: 3493 reply_id: 11239[/import]

Oh Great,
Thanks for that. I guess I need to spend some time with LUA.

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3493 reply_id: 11242[/import]

Ok… try the following here: http://www.lua.org/cgi-bin/demo

[lua]variable_1=“Hello”
variable_2=“world”
variable_3=“really”
variable_4=“cruel”
variable_5=“cold”
variable_6=“world”
variable_7=“greetings”
variable_8=“to”
variable_9=“you”

for i=1,9 do
print(_G[“variable_”…i])
end[/lua]
Edit: I edited the code to eliminate redundancy. No need for pairs here. Thanks firemaplegames! [import]uid: 7356 topic_id: 3493 reply_id: 11240[/import]

I’d recommend using your own table instead of the global table _G.

In your situation, you can reference keys in a dynamic fashion (or in an eval-like fashion if you’ve used Flash, circa 4.0/5.0)

[lua]local myTable = {
variable_1=“Hello”,
variable_2=“world”,
variable_3=“really”,
variable_4=“cruel”,
variable_5=“cold”,
variable_6=“world”,
variable_7=“greetings”,
variable_8=“to”,
variable_9=“you”,
}

for i=1,9 do
local key = “variable_” … i
print( myTable[key] )
end[/lua] [import]uid: 26 topic_id: 3493 reply_id: 11464[/import]

Hi Walter, Haven’t worked with Flash, so not very sure, is this more like a collection object or in terms of Objective-C, a Dictionary object?

can we also use the classByName or functionByName type of calls in Corona/Lua?

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3493 reply_id: 11466[/import]

Lua uses tables for a bunch of different things. It’s the workhorse object. They are a hybrid of arrays and dictionaries. They can behave like both.

There’s a tutorial here: http://lua-users.org/wiki/TablesTutorial

In terms of Obj-C, Lua tables have features of NSMutableDictionary as well as NSMutableArray, except you don’t access items by method calls

For NSDictionary-like behavior, you access key/value pairs as properties. For known keys, you can use the “dot” operator. For dynamic keys, you can use bracket notation:

[lua]-- create an empty table
local t = {}

t.myProperty = “foo”
print( t.myProperty ) --> foo
print( t[“myProperty”] ) -->foo[/lua]

For NSArray-like behavior, you access using numerical indices (just remember arrays in Lua are 1-based, not 0-based):

[lua]-- create an empty table
local t = {}
t[1] = 3
t[2] = 2
t[3] = 1

print( “counting down” )

– iterate thru all array members
#t is a way to get the length of the array (excludes dictionary key/value members)
for i=1,#t do
print( t[i] )
end
[lua] [import]uid: 26 topic_id: 3493 reply_id: 11534[/import]