I'm making some admin commands, and to add the :admin me command, I need to know how to add something to a table (because I use a table for my admin) Anyone know how?
To add something to a table you have a few choices:
array = {} --Make a table table.insert(array,1,"Value") --To add in a specific spot --or array[Value] = "Value2" --Add a specific value
to test this you can write:
print(array[1])
Output:
>Value
-
print(array[Value])
Output
>Value2
References: Table manipulation Tables
I've heard a lot that table.insert is actually slower than:
tab = {3, 4, 5, 6, 2, 12} -- 6 items tab[#tab + 1] = 5 print(tab[7]) -- print 7th item of tab
Output:
5
Why does this happen?
tab = {3, 4, 5, 6, 2, 12}
has 6 items/elements in it.
at this point if we printed
print(#tab)
we would get 6
tab[#tab]
would access the 6th item in tab which is 12, because #(table)
returns the amount of items/elements in a table, which is currently 6.
BUT..
tab[#tab + 1] = 5
This would get the amount of elements/items in our tab table which is 6 and then add 1 to that, and then make that element 5.
This would make the table:
tab = {3, 4, 5, 6, 2, 12, 5}
This is why it happens.