How to rename a table name

Hi everybody,

Does anyone know how to rename a table name with a string that you define earlier in a script?
local tab1={}

local myString =“mystrtable”

–here this string will be changed in script with other string so I want to have a dynamic table name.
–[[
how to rename tab1 with the value of myString ???

]]-- [import]uid: 65047 topic_id: 12079 reply_id: 312079[/import]

I don’t think you can rename a table after you have created it, if you can, someone will correct me.

I would suggest creating a new table with the appropriate name, copying the data from the original to the new table, then setting the original table to nil, so it gets GC’d. [import]uid: 5317 topic_id: 12079 reply_id: 44141[/import]

Just add a name field to your table.

tab1 = {}  
...  
tab1.name = "mystrtable"  

You can also assign multiple references to the same table (e.g. tab2 = tab1). It’s all in here:

http://www.lua.org/pil/2.5.html [import]uid: 58455 topic_id: 12079 reply_id: 44168[/import]

I think this is what you want to accomplish :

Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio  
\> table = {test = "value"}  
\> str = "myString"  
\> func = assert(loadstring(str .. "= table"))  
\> func()  
\> print(myString.test)  
value  

here, the secret is “loadstring()”… “assert” is used for catching errors if loadstring fails (ie. syntax errors) according to lua book… [import]uid: 46529 topic_id: 12079 reply_id: 44313[/import]

i think loadstring is disabled in corona. [import]uid: 48521 topic_id: 12079 reply_id: 44321[/import]