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

How would I insert the i and v ?

Asked by 5 years ago
local Tab = {Tab = 'G',S = 'CCC'}


local Table = {}

for i,v in pairs(Tab) do



end


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




I want Table to be exactly like Table but I dont know how to do it

like I want Table to be = to {Tab = 'G', S = 'CCC'}

but table.insert cant do this so im confused

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

You would just do this:

for k,v in pairs(Tab) do
    Table[k] = v
end

I changed the first loop variable from i to k because it's more conventional to use k for "key" for a dictionary, so that it's clear to someone reading your code that it's a dictionary not an array. Using i is the convention for "index", when looping over an array with ipairs, or as an integer counter a for loop.

This looping over a dictionary to copy keys and values is what's known as a "shallow copy", because if any values in Tab are themselves tables you will not be duplicating their contents, only creating references to them in Table. You can wrap this loop in a function though, and call it recursively to do a "deep copy" should you find yourself with this problem.

Ad

Answer this question