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]
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:
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
[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]
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]
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]
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:
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]