Convert variable name to a string?

We are passing some parameters into an external module such as:

{myVar = mNav}

In the external module we make bm = to myVar

local bm = params.myVar

Now bm = mNav

We use mNav as a variable to do stuff within the module, but we would also like to have a string version “mNav” so we can call functions such as funcbm. We can’t call this unless the value of bm is defined within speech marks such as “mNav”

Is there an easy way to turn the name of bm into a string e.g. mNav into “mNav”?

Can you rephrase your question? It is kind of confusing and didn’t understand what you need.

Ok, to break it down simple we have:

local bm = mNav

Is there a way to convert bm so that it outputs the name of the variable mNav (instead of mNav’s value or contents)

ie… the same as:

local bm = “mNav”

Ok. Got it. Being honest, I would rewrite the code to not need that… 

what is mNav
a number, table other
what does mNav hold

Thanks for taking a look…

mNav doesn’t hold anything, it is a name which determines tables to use e.g

bm = mNav

image = {}

image.mNav = { data }

image.bNav = { data }

image.jNav = {data}

for 1 = 1, #image[bm] do…

and runs specific functions

func = {}

func.mNav = function ()

funcbm

perhaps if you showed a bit more complete code it would be easier for me to understand. as your code is above there’s no reason you couldn’t just do bm = “mNav”

Thanks for taking a look. It doesn’t sound like you can easily take a variable name and convert it to a string with parentheses. So we will try it another way.

But looking at this particular problem, imagine you have an external list of names without parentheses around each one. How can you read this list in and use each one to call a function with the same name. ie - a list like

peter

paul

mary

david

eddie

Now we want to iterate through to number 3 and use an entry to call the function

func.mary = function ()

It would be called - func"mary"

But we don’t have “mary” in our list, we have mary.

that is exactly what @jstrahan said you to do,  use “mary” instead of mary in your list.

Variables serves to store values, if you have a variable that does not store any value, it does not make sense and you are wasting memory.

@renato is correct. what i’m trying to do is help you recode it so it will do what you need but without you showing some complete code its hard to do. it doesn’t have to be the code to your app if you don’t want to share just something short and similar so we can see how you are doing the code and show you how to recode for it to work. also include your code inside “[“code”]”  “[”/code"]" tags leaving off the " so it can be formatted for easier reading

Yeah but the list we are drawing from is not formatted that way. It is an external file with a list containing entries without parentheses. So we want to use “mary” but our list has mary instead. We can’t change the way the list has been formatted without manually adding parentheses to each of the hundreds of entries… That is why we wondered if there was a way to convert mary into “mary”.

so is the list like this

mNav = {john,mary,david}

or like this

mNav = {john=3, mary ="how", david = john}

Thanks again for the help guys, really appreciate it. We have found a different way to approach our problem, but the question here has been answered. Is there a way to turn a variable name into a string with parentheses? No. I did find a couple of other similar questions on the boards here:http://forums.coronalabs.com/topic/5344-converting-variable-name-to-a-stringhttp://forums.coronalabs.com/topic/14070-converting-variable-name-to-string/But neither of those shed light as to an easy solution.

if bm = “mary” and you have func.mary = function ()…end

then you can call

funcbm

By definition, t.mary == t[“mary”]. t.whatever is just syntactic sugar for t[“whatever”].

Not sure if that’s what you want…

  • C

Can you rephrase your question? It is kind of confusing and didn’t understand what you need.

Ok, to break it down simple we have:

local bm = mNav

Is there a way to convert bm so that it outputs the name of the variable mNav (instead of mNav’s value or contents)

ie… the same as:

local bm = “mNav”

Ok. Got it. Being honest, I would rewrite the code to not need that… 

what is mNav
a number, table other
what does mNav hold

Thanks for taking a look…

mNav doesn’t hold anything, it is a name which determines tables to use e.g

bm = mNav

image = {}

image.mNav = { data }

image.bNav = { data }

image.jNav = {data}

for 1 = 1, #image[bm] do…

and runs specific functions

func = {}

func.mNav = function ()

funcbm