Dynamically assigning table names

Woops, I think topic title should be “Dynamically accessing table names

sorry, newbie here.

Is it possible in Corona/Lua to assign a variable name to represent a table name ?

As the user clicks on a screen item - I want that items name to represent the name of the table being called.

I have 2 lines:

 

myName = (seedTable[i].seedType)

 

This line grabs the seed name from a pre-made table, and returns names like strawberry, orange, etc. 

Strawberry, orange etc,  are also the names of other pre-made tables. 

 

I then want to use myName to dynamically access different tables depending on the name returned.

 

i.e. in a line like this…

 

if (myName[count] == true) then

 

This never seems to work, I can only seem to manually access each seed table as below…

 

**if (strawberry[count] == true) then   or   if (orange[count] == true) then   ,   **etc

 

Also…

If I have tables within tables , i.e.:

 

planting = {}

planting.loc1 = {multiple data items}

planting.loc2 = {multiple data items}

planting.loc3 = {multiple data items}

etc…

 

I have been trying to loop through using a line such as

 

for i = 1, #planting do

    if (planting.loc[i].occupied == false) then

        – e.g, seed location unavailable for planting

    end

end

 but no luck.

 

Is there a way to use variables or a type of concatenation to achieve the result I’m looking for ?

 

Any help would be very much appreciated!

Matt.

I recommend putting that in a table where the fruit names are keys.

fruits = {} fruits.orange = {} fruits.orange.count = 10 fruits.orange.seedType = 3 etc...

Then you can easily access orange seedType with:

someVar = "orange" print(fruits[someVar].seedType)

Your example with planting.loc1…2…3 thing. It would make sense to have a table like this (assuming you are putting more into that planting table than just locations):

planting = {} planting.locations = {} planting.locations[1] = {} planting.locations[1].occupied = false etc..

To loop through locations you can then do close to what you had:

for i = 1, #planting.locations do     local occupied = planting.locations[i].occupied     if (occupied == false) then         -- e.g, seed location unavailable for planting     end end

When you start naming variables like something1, something2, something3 etc then somethings not right.

Thanks so much Jon, that worked brilliantly !!!     :slight_smile:

So if I understand it right. I can’t use _ someVar _ for the topmost level table i.e.: _ fruits _

print(someVar[orange].seedType)  – this is bad ?

But for any tables within _ fruits _ it’s ok.

print(fruits[someVar].seedType)    – this is good ?

Thanks again…

Good to hear.

Yes you can use someVar for a table key names, but if you want to use it as a table name or variable name itself you have to start doing workarounds in Lua.

Ah I see. 

Do you have a link to any documentation on how to do the workarounds that you have mentioned.

It really could be quite useful.

Or can you give me a short rundown if you have time ?

PS: so happy to have my code working now though !!!

If someVar is global its easy. Globals reside in table called _G. So you can get it with _G[“someVar”]. It is hacky though, should not even have a global to begin with.

Not sure about workaround if its a local.

Ah, got it. Thanks.

Yeah, I’ll stick to your initial solution, that’s works great.

Won’t mess around with globals.

Really appreciate your time Jon, you’re a life saver.

Matt :slight_smile:

I recommend putting that in a table where the fruit names are keys.

fruits = {} fruits.orange = {} fruits.orange.count = 10 fruits.orange.seedType = 3 etc...

Then you can easily access orange seedType with:

someVar = "orange" print(fruits[someVar].seedType)

Your example with planting.loc1…2…3 thing. It would make sense to have a table like this (assuming you are putting more into that planting table than just locations):

planting = {} planting.locations = {} planting.locations[1] = {} planting.locations[1].occupied = false etc..

To loop through locations you can then do close to what you had:

for i = 1, #planting.locations do     local occupied = planting.locations[i].occupied     if (occupied == false) then         -- e.g, seed location unavailable for planting     end end

When you start naming variables like something1, something2, something3 etc then somethings not right.

Thanks so much Jon, that worked brilliantly !!!     :slight_smile:

So if I understand it right. I can’t use _ someVar _ for the topmost level table i.e.: _ fruits _

print(someVar[orange].seedType)  – this is bad ?

But for any tables within _ fruits _ it’s ok.

print(fruits[someVar].seedType)    – this is good ?

Thanks again…

Good to hear.

Yes you can use someVar for a table key names, but if you want to use it as a table name or variable name itself you have to start doing workarounds in Lua.

Ah I see. 

Do you have a link to any documentation on how to do the workarounds that you have mentioned.

It really could be quite useful.

Or can you give me a short rundown if you have time ?

PS: so happy to have my code working now though !!!

If someVar is global its easy. Globals reside in table called _G. So you can get it with _G[“someVar”]. It is hacky though, should not even have a global to begin with.

Not sure about workaround if its a local.

Ah, got it. Thanks.

Yeah, I’ll stick to your initial solution, that’s works great.

Won’t mess around with globals.

Really appreciate your time Jon, you’re a life saver.

Matt :slight_smile: