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

I don't understand the logic of how doing #Tab + 1 is adding something?

Asked by 5 years ago
Edited by RubenKan 5 years ago

I don't understand the logic behind this

local Tab = {}

Tab[#Tab + 1] = 'Value'

for i,v in pairs(Tab) do
    print(i,v)
end

Why do you have to put #Tab number of Tab + 1 why cant you just do Tab + 1

I heard this is also similar to table.insert

1 answer

Log in to vote
0
Answered by 5 years ago

Putting # in front of a table will return the number of objects it has in it. If you did "Tab + 1" it would be like "{} + 1". But doing #Tab + 1 is like doing "0 + 1" since there are currently no objects in the table!

TableName gets the number of objects in the table. TableName returns the table.

Ad

Answer this question