luaopencomputers

Lua table keys changes in function return


It seems when I return a table from a function I lose they keys. Not sure if this is how Lua should be functioning.

For example

function main()
  local someTable = {}
  someTable["foo"] = "bar"
  print(someTable["foo"])
  return someTable
end

local test = main()
print(test["foo"])
for k, v in pairs(test) do
  print(k, v)
end

bar
nil
1 bar

Solution

  • Your code is ok and shows the expected behaviour in a standard Lua environment like the Lua Online Demo.

     bar
     bar 
     foo
     bar
    

    So either there is an issue with the environment you're running that script in or there is a difference between the code you posted here and the code you're running ony our machine.