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

[Lua][Question]Table with unknown name

Started by Drifter, 21 April 2013 - 03:09 AM
Drifter #1
Posted 21 April 2013 - 05:09 AM
Hi, I am just wondering if you can make table based on read() input.
example:

name = read()
- I enter for example: fruit
- then I want make table with name fruit, but I don't know how. I can't do this:

fruit = {}
, because I don't know what will input be. I also can't do this:

name = {}
, because this create table name, not fruit.

Is this possible? And if yes, how can I do it, and how can I access this table.

Thnx.
Engineer #2
Posted 21 April 2013 - 05:32 AM
I do not know the in-depth logic behind it, but you want to use this:

local var = read()
local code, err = loadstring("local " .. var .." = {}")
code()
--Do stuff with the table
Edited on 21 April 2013 - 03:32 AM
Bubba #3
Posted 21 April 2013 - 07:48 AM
Loadstring should be avoided where possible (it's associated with performance overhead in actual Lua, although I have no idea about LuaJ). Use the _G table instead:

local input = read()
_G[input] = {}

The _G table is the global environment, so if you add something to that then it will be accessible everywhere. Another advantage to this over the loadstring method is that people won't be able to make malicious code and execute it.

If my input is "aTable" then the above code is exactly the same as doing:

aTable = {}

You can access it the same as you would any table.

Just out of curiosity, why do you need this?
theoriginalbit #4
Posted 21 April 2013 - 07:54 AM
Another way you could do it is store the tables inside another table (which is the _G way Bubba stated, just local not global)

local tTables = {}
local input = read()
tTables[input] = {}

I am also curious though, why do you need this? why can't you just have a defined table name?
remiX #5
Posted 21 April 2013 - 08:10 AM
Maybe he wants admins to be able to create new user groups for a login script or something :P/>
theoriginalbit #6
Posted 21 April 2013 - 08:16 AM
So you just have a table of groups. Why would you have individual tables for the groups, of which you would need to remember each's name to get back to it later, instead of just one master table which you can iterate through.
remiX #7
Posted 21 April 2013 - 09:18 AM
I was just taking a guess, sheesh :P/>

Idk, maybe he wants each group in a separate table
Drifter #8
Posted 21 April 2013 - 10:05 AM
Why I need this?
I have my own API for buttons (like direwolf20), but in my API you can specify everything (for example, color of background, color of text, code which will run when pressed, if the button is toggle or pulse…)
And first parameter is group, you must add button in group. I think it is usefull, you can have 3 buttons in one group and 5 in another. Now you can hide one group or display one group if another button is presses.

The way the buttons are stored is:

group = {["name"]={...a lot of properties...},["another button"]={..a lot of properties..}}

Now, I am creating user-friendly interface program for making buttons. User just enter group, text in button and click on monitor, to specify where the button will be. At the end computer will display code for him, now he can just copy and paste this code into his program. Problem is at begining I don't know group name.
theoriginalbit #9
Posted 21 April 2013 - 07:51 PM
ok then just do what I said and have all the groups in a table….

local buttonGroups = {
  <group> = {
	<button>,
	<button>,
	<button>,
  },
  <group> = {
	<button>,
	<button>,
  },
  etc
}