This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
darkrising's profile picture

[lua] [solved] table name?

Started by darkrising, 18 October 2012 - 10:57 PM
darkrising #1
Posted 19 October 2012 - 12:57 AM
This maybe a silly question but I'm still learning. is there a function or shortcut to return a tables name as a string?
aTable = {"data","data","something else"}

i just want "aTable" as a string instead of a table so when I do print() it returns "aTable" instead of table: (random letters)
Noodle #2
Posted 19 October 2012 - 12:58 AM
aTbl = textutils.serialize(aTable)
print(aTbl)
darkrising #3
Posted 19 October 2012 - 01:02 AM
aTbl = textutils.serialize(aTable)
print(aTbl)

That just returns the data in the table :P/>/>
Noodle #4
Posted 19 October 2012 - 01:03 AM
aTbl = textutils.serialize(aTable)
print(aTbl)

That just returns the data in the table :P/>/>
Well..
print(aTable[1])
or
aTable = {test = "String" }
print(aTable.test)
MysticT #5
Posted 19 October 2012 - 01:05 AM
No there's no way to get the table name, because when compiled to bytecode the name is not used anymore. But, if that table is global (or inside another table) you can do it by comparing it to each value in _G (or whatever table it's stored in).
Example:

local function getTableName(tbl)
  for k, v in pairs(_G) do
    if v == tbl then
	  return k
    end
  end
  return nil
end

someTable = {}
print(getTableName(someTable))
Very bad thing to do, but it should work.

What do you even need this for? There might be a better way to do it.
darkrising #6
Posted 19 October 2012 - 01:07 AM
aTbl = textutils.serialize(aTable)
print(aTbl)

That just returns the data in the table :P/>/>
Well..
print(aTable[1])
or
aTable = {test = "String" }
print(aTable.test)

Well I guess I could just add to all my tables aTable {Name = "tablename"} but is there no other shorter way?
Noodle #7
Posted 19 October 2012 - 01:08 AM
aTbl = textutils.serialize(aTable)
print(aTbl)

That just returns the data in the table :P/>/>
Well..
print(aTable[1])
or
aTable = {test = "String" }
print(aTable.test)

Well I guess I could just add to all my tables aTable {Name = "tablename"} but is there no other shorter way?
When you print table, you have to do it in a loop for all values or just print those single values.
Else, you just print(aTable) and it comes with the variable assigned for that table.
darkrising #8
Posted 19 October 2012 - 01:10 AM
No there's no way to get the table name, because when compiled to bytecode the name is not used anymore. But, if that table is global (or inside another table) you can do it by comparing it to each value in _G (or whatever table it's stored in).
Example:

local function getTableName(tbl)
  for k, v in pairs(_G) do
	if v == tbl then
	  return k
	end
  end
  return nil
end

someTable = {}
print(getTableName(someTable))
Very bad thing to do, but it should work.

What do you even need this for? There might be a better way to do it.

Ok, good to know, I wrote a function which saves tables in files, but its silly having to type Array_SAVE("passwords", passwords)


function Array_SAVE(FileName, ARRAY)
  local collection = ""

  for i=1, #ARRAY do
    if i==1 then
      collection = (collection..[["]]..ARRAY[i]..[["]])
    else
      collection = (collection..", "..[["]]..ARRAY[i]..[["]])
    end
  end

  local file = io.open(FileName,"w")
        file:write(FileName.." = {"..collection.."}")
        file:close()  
end
MysticT #9
Posted 19 October 2012 - 01:15 AM
Well, I think it's better to do it that way (passing the name and the table). Also, you should try using textutils.serialize and unserialize, it will make it easier to save and then load.
darkrising #10
Posted 19 October 2012 - 01:18 AM
Well, I think it's better to do it that way (passing the name and the table). Also, you should try using textutils.serialize and unserialize, it will make it easier to save and then load.

ok, thanks for the help, I'm working on quite a big server program which handles a lot of arrays: whitelists, passwords, ids etc, just trying to trim the fat as it were :P/>/>