Initialise table with keys with for loop

55 views Asked by At

Is it possible to initialise a Lua table which has keys and values with a for loop? I tried it like this, but of course it is creating a new key called "name" instead of referring to the for variable "name".

local mytable = {A = "", B = "", C = ""} 

function main() 
    for name, value in pairs(mytable) do    
        mytable.name = math.random(1000)
    end      
    OutputTable(mytable)
end

function OutputTable(t)
    for name, value in pairs    (t)     do
      print(name,value)
    end
end

main()

Output:

B   
name    754
A   
C
0

There are 0 answers