2016-09-08 48 views
2

我有兩個值的集合來定義:成對在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... 

任何人都可以幫助我以正確的方式編碼嗎?

回答

3

您可以輕鬆地用一個數字環路做到這一點:

for i = 1, 4 do 
    local a, b = A1[i], B1[i] 
    --- use them 
end 

你去確定你需要的迭代次數是怎樣的棘手的部分。如果尺寸不同,但每個表的長度與其他尺寸相同,則可使用長度運算符(#A1)。

或者,您可能需要一個返回給定表集的最大長度的函數。

local function max_table_len (...) 
    local tabs = { ... } 
    local len = 0 

    for i = 1, #tabs do 
     local l = #tabs[i] 

     if l > len then 
      len = l 
     end 
    end 

    return len 
end 

甚至可能是一個幫助函數來獲取每個值。

local function get_from_tables (index, ...) 
    local values = { ... } 
    local len = #values 

    for i = 1, len do 
     values[i] = values[i][index] 
    end 

    return table.unpack(values, 1, len) 
end 

結束了的東西,如:

for index = 1, max_table_len(A1, B1) do 
    local a, b = get_from_tables(index, A1, B1) 
end 
+0

在[Lua的手冊](https://www.lua.org/manual/5.3/manual.html#3.3.5),即呼叫一個「數字for循環」,另一個形成「通用for循環」。 –

+0

@TomBlodget良好的捕獲,歡呼聲。 – Oka

1

您可以在ipairs例如修建從Programming in Lua。例如,這個版本並行迭代2個序列:

-- iterator function 
local function iter_ipairs2(tablePair, i) 
    i = i + 1 
    local v1 = tablePair[1][i] 
    local v2 = tablePair[2][i] 
    -- if you use 'and' here the iteration stops after finishing 
    -- the shortest sequence. If you use 'or' the iteration 
    -- will stop after it finishes the longest sequence. 
    if v1 and v2 then 
    return i, v1, v2 
    end 
end 

-- this is the function you'll call from your other code: 
local function ipairs2(t1, t2) 
    return iter_ipairs2, {t1, t2}, 0 
end 

-- usage: 
local A1 = {100, 200, 300, 400, 500} 
local A2 = {500, 600, 700, 800} 

for i, v1, v2 in ipairs2(A1, A2) do 
    print(i, v1, v2) 
end 
0

以前的答案更詳細,並提供更一般和更好的答案。

這是一個非常新的Lua的人。它不僅顯示出兩個循環,而且強化了通常有多種方式可以到達您想要去的地方。

local A1 = {100, 200, 300, 400} 
local A2 = {500, 600, 700, 800} 

print("simplest answer:") 
-- doesn't use ipairs and assumes A1 and A2 are the same size 
for i = 1, #A1 do     
    B1 = A1[i] 
    B2 = A2[i] 
    print(B1, B2, "(iteration #"..i..")") 
end 

print() 

print("answer that uses ipairs:") 
-- again, assumes A1 and A2 are the same size 
for i, v in ipairs(A1) do 
    B1 = A1[i]  -- i steps through A1 and A2 
    B2 = A2[i]  -- this works because A1 and A2 are same size 
    print(B1, B2, "(iteration #"..i..")") 
end 

給出這個輸出:

simplest answer: 
100 500 (iteration #1) 
200 600 (iteration #2) 
300 700 (iteration #3) 
400 800 (iteration #4) 

answer that uses ipairs: 
100 500 (iteration #1) 
200 600 (iteration #2) 
300 700 (iteration #3) 
400 800 (iteration #4)