This is one solution using an iterator:
function two(t) local i = -1 return function() i = i + 2; return t[i], t[i + 1] end end
Then you can use the iterator as follows:
local bar = {} for k, v in two(foo) do bar[k] = v end
Note that this should be bar = {[key1]=value1, [key2]=value2} . In your example, {key1=value1,key2=value2} is the syntactic sugar for string keys.
Yu Hao
source share