我有兩個值的集合來定義:成對在LUA程序設計
local A1 = {100, 200, 300, 400}
local A2 = {500, 600, 700, 800}
我想要迭代的循環爲另一個變量B1和B2從A1和A2對分配值如下:
B1 = 100 and B2 = 500 (first iteration)
B1 =200 and B2 = 600 (second iteration)
B1 = 300 and B2 = 700 (third iteration)
B1=400 and B2 = 800 (fourth iteration)
我試圖用ipairs如下:
for i, f1 in ipairs(A1) do
for j, f2 in ipairs(A2) do
B1 = f1
B2 = f2
end
end
但是這給了我
B1 = 100 and B2 = 500 (first iteration)
B1 =100 and B2 = 600 (second iteration)
B1 = 100 and B2 = 700 (third iteration)
B1=100 and B2 = 800 (fourth iteration)
B1 = 200 and B2 = 500 (fifth iteration)
B1 =200 and B2 = 600 (sixth iteration)
B1 =200 and B2 = 700 (seventh iteration)
....
...
...
so on...
任何人都可以幫助我以正確的方式編碼嗎?
在[Lua的手冊](https://www.lua.org/manual/5.3/manual.html#3.3.5),即呼叫一個「數字for循環」,另一個形成「通用for循環」。 –
@TomBlodget良好的捕獲,歡呼聲。 – Oka