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

I don't understand how using brackets on tables can mean inserting?

Asked by 4 years ago
local tab = {}

local key = "Potatoes"
local value = "are cool"

tab[key] = value

-- or you could do:
tab.Potatoes = "are cool"
-- or
tab["Potatoes"] = "are cool"

print(tab[key]) -- prints: are cool
print(tab.Potatoes) -- prints: are cool
print(tab["Potatoes"]) -- prints: are cool


for key, value in pairs(tab) do
    print(key, value)
end

-- since we have a "Potatoes" key with "are cool" as its value, it would print:
-- Potatoes are cool

on line 6 it saids tab[key] = value

but i dont understand what this does

Its like inserting but I dont understand how this inserts

shoudl't doing this change tabs key into value which is 'are cool'

but it dosen't it prints Potatoes are cool and merges it

1 answer

Log in to vote
1
Answered by 4 years ago

This is because it adds an index to the table, it sets the index to the table

Ad

Answer this question