Variable to string

I don’t know if this is a stupid question, but I can’t seem to find an answer.
Is there a way to convert a variable name to a string? i.e. can we print the variable name and not just it’s value?
For example:

x = 1
function(x) --should return "x" not 1

@MattthewLXXIII Just to give you a quick answer, I would say no. When you pass a variable to a function, you’re not passing the variable name, you’re passing the memory address of that variable. Codea looks in it’s table of variables and finds the address associated with that variable. It then passes that address to the function and the function uses that address to get the value stored there. At this point I don’t know if there’s a way to search Codea’s table using the address to get the variable name. Does that sound like what you’re after because I’m not completely sure what you’re trying to do.

@MattthewLXXIII Can you give a better example of what you’re trying to do. Where are you getting or how are you getting the variable names that you’re passing to a function. It doesn’t sound like you’re hard coding them because if you are, you could just make the variable a string to begin with.

@dave1707 Yeah, I could, I was just being lazy. I will have a table of maps = {CLASSIC, PLAIN} et cetera. Then I’ll these variables to call functions to make the walls, like Map:maps[mapnumber]() where Map() is a class. In my settings I wanted to change mapnumber but showing the actual name of the map e.g. text(mapnames[mapnumber]), where mapnames = {"CLASSIC", "PLAIN"}, I just didn’t want to have to change both maps and mapnames every time I added a new map.
I guess I spent more time typing this out than I would have if I just did both XD.

EDIT: I just realised what I wanted should not be possible, sorry for wasting your time.

@MattthewLXXIII I’m not sure if this is what you’re trying to do. You would add new names to self.maps. Since you have to code those functions anyways, just add a print/text statement with their names.

function setup()


function Map:init()

function Map:map(mapnumber)

function Map:CLASSIC()
    print("the function classic was executed")

function Map:PLAIN()
    print("the function plain as executed")

It was somewhat similar, I just didn’t want to have to write out "CLASSIC" for the print, the other stuff is pretty much what I’m going to have. Thanks for your help anyway.

@MattthewLXXIII - how about

maps = {CLASSIC=map[1],PLAIN=map[2]) --ie a named table
CurrentMap=maps.CLASSIC --sets map[1] as current map
--if you create a property in the maps class containing the map name, then
--you can write the map name with

Thanks guys, I sort of combined both of your responses.

If you have a dictionary, the keys to the dictionary are strings that can be printed.

maps = {CLASSIC = Map(1), PLAIN = Map(2)}

is short-hand for:

maps = {["CLASSIC"] = Map(1), ["PLAIN"] = Map(2)}

Though it’s hard to access the keys as strings except by iterating:

for key, map in pairs(maps) do

And unlike arrays, dictionaries have unpredictable ordering.

I would just add a “name” property to your Map class.

@MattthewLXXIII I think it will be easier and a lot less keying to just create the table of mapnames. How many mapnames are you thinking of having.

@dave1707 Yeah, I just hardcode them. Not too many, maybe 10.

@MattthewLXXIII I could understand your wanting to find a shortcut if you were adding hundreds of mapnames on a regular basis, but 10 mapnames isn’t a problem for 2 tables. Trying to find a solution for your question was interesting anyways. Thanks for asking.

Haha, yeah, I just thought it would be nice if I didn’t have to hardcode stuff more than once, however small the things to write.

@MattthewLXXIII I don’t know if you need this anymore, but this code prints the function name and executes that function using 1 table like you requested. I print the function name and then I use a print statement to show that the function is being executed.

EDIT: Changed original program to create nameTab in setup().

function setup()    
    local str=readProjectTab("main")
    for a in string.gmatch(str,"tab={(%g+)}") do
        for b,_ in string.gmatch(a,"(%a+)([,]*)") do

function exec(v)

function CLASSIC()
    print("classic function was called\

function PLAIN()
    print("plain function was called\

function NORMAL()
    print("normal function was called\

function SPECIAL()
    print("special function was called\

Sorry, I probably won’t end up using it; I just hard coded it anyway.

@MattthewLXXIII I figured you wouldn’t. I just wanted to show that it could be done. Your question was a good challenge and I didn’t want to give up until I got it to work or was convinced it couldn’t be done.