我在LOOP1(CHECK1)嵌套循環2(CHECK2),但它似乎嵌套循環2(CHECK2)只運行一次。紅寶石嵌套循環
兩個循環都包含相同的數組。該腳本用於檢查check1中的重複ID。
check1=["0", "0", "0", "1", "1", "2", "3", "4", "5", "100", "4294967294", "9", "11", "6", "200", "7", "201", "811", "202", "204", "3000", "205", "3001", "3001", "3001"]
check2 =["0", "0", "0", "1", "1", "2", "3", "4", "5", "100", "4294967294", "9", "11", "6", "200", "7", "201", "811", "202", "204", "3000", "205", "3001", "3001", "3001"]
代碼:
check1.each do |check1|
counter=0
puts "checking against:"+check1.to_s
check2.each do |check2|
puts "checking:"+check1.to_s+"|"+check2.to_s
if check1 == check2
counter += 1
end
end
if counter > 1
dupUID << check1
end
end
結果:
checking against:0 <- checking the 1st element in check1
checking:0|0
checking:0|0
checking:0|0
checking:0|1
checking:0|1
checking:0|2
checking:0|3
checking:0|4
checking:0|5
checking:0|100
checking:0|4294967294
checking:0|9
checking:0|11
checking:0|6
checking:0|200
checking:0|7
checking:0|201
checking:0|811
checking:0|202
checking:0|204
checking:0|3000
checking:0|205
checking:0|3001
checking:0|3001
checking:0|3001
checking against:0<- checking the 2nd element in check1
checking:0|3001 <- nested loop2(check2) is not looping again on the 2nd element of loop 1
checking against:0
checking:0|3001 <- loop2 stops at the last element for the remaining elements in check1
checking against:1
checking:1|3001
checking against:1
checking:1|3001
checking against:2
checking:2|3001
checking against:3
checking:3|3001
checking against:4
checking:4|3001
checking against:5
checking:5|3001
checking against:100
checking:100|3001
checking against:4294967294
checking:4294967294|3001
checking against:9
checking:9|3001
checking against:11
checking:11|3001
checking against:6
checking:6|3001
checking against:200
checking:200|3001
checking against:7
checking:7|3001
checking against:201
checking:201|3001
checking against:811
checking:811|3001
checking against:202
checking:202|3001
checking against:204
checking:204|3001
checking against:3000
checking:3000|3001
checking against:205
checking:205|3001
checking against:3001
checking:3001|3001
checking against:3001
checking:3001|3001
checking against:3001
checking:3001|3001
任何人都可以指出我的錯誤?非常感謝。 解決:謝謝大家!
check1.each do |ch1|
counter=0
check2.each do |ch2|
if ch1 == ch2
counter += 1
end
end
if counter > 1
dupUID << ch1
end
end
puts dupUID
我認爲你的算法是有缺陷的。你爲什麼要比較兩個相同的陣列呢?即使使用你的方法,爲了找到重複,你需要檢查'counter> 1',而不是'counter> 0',因爲它們是相同的,因爲總是會有另一個數組中的1個匹配元素。如果你能解釋你在這裏做什麼,我相信有更好的方法來做到這一點。 – Anurag 2010-07-30 02:22:14
他試圖在一個數組中找到重複的值。這個想法並不是那麼糟糕,執行會留下一些需要的東西。 – Amadan 2010-07-30 02:26:25
嗨,Anurag是正確的,它應該是counter> 1.我需要在check1中找到重複的id。這意味着0,1,3001應該標記爲dupUID – cherhui 2010-07-30 02:31:22