Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to add something to a table?

Asked by 9 years ago

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?

0
table.insert User#9949 0 — 9y
0
im hungry HungryJaffer 1246 — 9y
0
im turtle User#9949 0 — 9y

2 answers

Log in to vote
1
Answered by
drahsid5 250 Moderation Voter
9 years ago

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

Ad
Log in to vote
0
Answered by 9 years ago

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.

Answer this question