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:
1 | array = { } --Make a table |
2 |
3 | table.insert(array, 1 , "Value" ) --To add in a specific spot |
4 |
5 | --or |
6 |
7 | array [ Value ] = "Value2" --Add a specific value |
to test this you can write:
1 | print (array [ 1 ] ) |
Output:
1 | >Value |
-
1 | print (array [ Value ] ) |
Output
1 | >Value 2 |
References: Table manipulation Tables
I've heard a lot that table.insert is actually slower than:
1 | tab = { 3 , 4 , 5 , 6 , 2 , 12 } -- 6 items |
2 | tab [ #tab + 1 ] = 5 |
3 | 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..
1 | 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:
1 | tab = { 3 , 4 , 5 , 6 , 2 , 12 , 5 } |
This is why it happens.