Converting variable name to string

I want to get the name of the variable (seen below as testvar) and convert it to a string.

[lua]testvar = astring

print("testvar: "…tostring(testvar))[/lua]

returns nil.

[import]uid: 79135 topic_id: 17244 reply_id: 317244[/import]

In your code, “testvar” holds the value of the variable astring. It appears you have not initialized the variable named astring with anything and therefore it is nil. When that code executes, nil is copied from astring to testvar and when you try to print it you get nil.

I suspect that astring is supposed to be a string, i.e.

testvar = “a string”
and for what its worth, you don’t have to use tostring() around variables to print them.

I’m not sure how to tell you to get the variable name into a string. [import]uid: 19626 topic_id: 17244 reply_id: 65114[/import]

I just want the variables name, not it’s data [import]uid: 79135 topic_id: 17244 reply_id: 65115[/import]

This is just a wild idea but how about creating a table. A bit round about way of doing things, but it might just serve the purpose you need it for? See below.

Naomi

[lua]-- I want myVariable to have a value of an integer, but I also want to know the name of myVariable:
local myVariable = {}
myVarialbe.value = 0
myVariable.name = “myVariable”
–print("myVariable name is "… tostring(myVariable.name))
print("myVariable name is "… myVariable.name)[/lua]

Quick edit to the print statement. [import]uid: 67217 topic_id: 17244 reply_id: 65120[/import]

What exactly are you trying to do in the long run? Getting the name of a variable seems like a pointless idea, unless there is some context to it. [import]uid: 49447 topic_id: 17244 reply_id: 65123[/import]

That’s where you use arrays or dictionary objects.

 local data = {}  
  
 data["myvar"] = "MyVar"  
 data["thisOne"] = "This One"  
 local theVar = "myvar"  
 print(data[theVar])  

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17244 reply_id: 65148[/import]