Ruby嵌套循环

时间:2010-07-30 01:58:37

标签: ruby loops nested-loops

我在loop1(check1)中有一个嵌套的loop2(check2),但似乎嵌套的loop2(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

5 个答案:

答案 0 :(得分:2)

我猜你不应该写这样的代码。有一个更好的解决方案:

x = [0, 10, 15]
y = [0, 20, 15]
x & y # => [0, 15] 

此方法返回指定的两个数组的公共元素。

(已更新)还有另一种方法可以在一个阵列中执行此操作:

[0, 10, 10, 15, 20].inject({}) 
{ 
    |a, c| a[c] ||= 0; a[c] = a[c].next; a 
}.delete_if { |k, v| v == 1 }.keys

答案 1 :(得分:2)

您正在隐藏check1check2数组,因为do块变量与它们具有相同的名称。

在内部do块之后,check2引用数组的最后一个元素,而不是数组本身。

要解决此问题,请将块变量重命名为ch1ch2

这就解释了为什么嵌套循环没有按预期运行。实际上,你的算法本身也存在缺陷。 @floatless的答案提供了一个更好的解决问题的方法。

答案 2 :(得分:1)

更短,但不是更容易理解:

check.inject(Hash.new(0)) { |a, x| a[x] += 1; a }.reject { |k, v| v <= 1 }.keys

答案 3 :(得分:0)

假设您有一个数组check,并且您想要查找其中的所有重复元素。使用Ruby 1.9。

check.group_by {|v| v}.map { |k, v| v.size > 1 ? k : nil }.compact

说明:

  • group_by返回一个Hash,其中key是数字,值是每次出现的数组。
  • 如果值只出现一次,则
  • map返回nil;如果值出现多次,则返回值。
  • compact清除所有nil值。

以下是逐步结果:

# after group_by
{"204"=>["204"], "6"=>["6"], "11"=>["11"], "205"=>["205"], "7"=>["7"], "811"=>["811"], "9"=>["9"], "4294967294"=>["4294967294"], "0"=>["0", "0", "0"], "100"=>["100"], "1"=>["1", "1"], "200"=>["200"], "2"=>["2"], "201"=>["201"], "3"=>["3"], "3000"=>["3000"], "202"=>["202"], "4"=>["4"], "3001"=>["3001", "3001", "3001"], "5"=>["5"]}>

# after map
[nil, nil, nil, nil, nil, nil, nil, nil, "0", nil, "1", nil, nil, nil, nil, nil, nil, nil, "3001", nil]

# after compact
["0", "1", "3001"]

答案 4 :(得分:0)

如果您($VERBOSE = true)上有警告,它会通知您有关您的错误。

IRB for ruby​​ 1.9.1不允许您打开$ VERBOSE at the command line,但1.9.2会打开。

更新:这个问题引导我为ruby提交this bug/feature improvement。谢谢!