Three loops but different results, why? (in lua 5.1)
1.
local a = {{b=5}, {b=4}}
for k,v in ipairs(a) do
v.b = v.b + 1
end
2.
local a = {["b"]=5, ["bb"]=4}
for k,v in pairs(a) do
v = v + 1
end
3.
local a = {5, 4}
for k,v in ipairs(a) do
v = v + 1
end
- 1 truly add 1 to all elements in table a, but 2&3 change nothing. why?
- I use chunkspy to see the op code of these three blocks found that in first block it has settable op after alter the value in table a, but block 2or3 has not. Block 2&3 just do add 5 5 261 ; 1 (means add 1 to register 5 but not save the value to table), why this happen?