How do you insert a full table into another table/How do you change the name of a table?


Original Question

I want to add say Table = {["Test Command"] = {"tableitem1","tableitem2"}, ["Extra Command"] = {"extra1","extra2"}} into the commands table, however every time I try to insert it; I get the index value. I know that table.insert() goes off of index values, so now the question is; how do you insert the test command into the Commands table?


Answer

Alright, my goal was; insert a bracket string table that has other tables inside of the string table, into one big table without using table.insert(). The reason people have issues inserting a bracket string table into another table (getting a number where the main title, in this case “Table Name”, would be.) is because when you use table.insert(), you’re only inserting the numbers of the index instead of sending the whole string. That’s why in this instance, if I referenced my original table; if I printed that out, I would of gotten [Index Number] = {"tableitem1","tableitem2"} or not have gotten anything at all.

The way to send the whole string you need is to as @PerilousPanther said;

How do you do that? Use the coding below this as reference to create/insert your bracket tables where you want them to be;

local TableReference = {}

TableReference["Table Name"] = {
	"Everything else you want inside the table", 
	Function = function() 
		print("Yes, even functions and other items as well.") 
	end}

Thank you @PerilousPanther for your assistance in this question.

I had no idea that you could add something into a table by doing what I did above; this will make things a LOT easier for me in the future.

for anyone wondering; the reason I was asking was because I wanted to create a self command creation function for my admin system.


1 Like

First of all, table.index can add values to the positional index you include as a parameter, but if you exclude that parameter it will just append the value to the table.

However, since you’re essentially naming the index by declaring “test command” you should treat the table as a dictionary. As such you could just do this:

local tab = {}

tab["Test Command"] = {tableitem1, tableitem2}
3 Likes

Why don’t you check and come back?

Right. I’m not totally sure what you’re trying to do there. Inheritance using brackets works the same as dots, so this works:

Commands["Child1"]["ChildofChild1"]

You can initialise a table like this:

local tab = {["thing1"] = {["thing1child1"] = {["etc."] = 1}}, 
                   ["thing2"] = "etc"}
1 Like