1

2つのインデックスを持つrubyforループを作成できますか?すなわち:

 for i,j in 0..100
     do something
 end

グーグルで何も見つかりません

編集:詳細を追加

私はそのような2つの異なる配列を比較する必要があります

Index:  Array1:  Array2:

   0       a        a
   1       a        b
   2       a        b 
   3       a        b
   4       b        b
   5       c        b
   6       d        b
   7       d        b
   8       e        c
   9       e        d
   10      e        d
   11               e
   12               e

しかし、両方が同じアイテムを持っていることを知っている(abcde)これは疑似の私の論理です、このすべてがループの中にあると仮定しましょう

#tese two if states are for handling end-of-array cases
If Array1[index_a1] == nil
    Errors += Array1[index_a1-1]
    break
If Array2[index_a1] == nil
    Errors += Array2[index_a2-1]
    break

#this is for handling mismach
If Array1[index_a1] != Array2[index_a2]

    Errors += Array1[index_a1-1] #of course, first entry of array will always be same

    if Array1[index_a1] != Array1[index_a1 - 1]
         index_a2++ until Array1[index_a1] == Array2[index_a2]
         index_a2 -=1 (these two lines are for the loop's sake in next iteration)
         index_a1 -=1

    if Array2[index_a2] != Array2[index_a2 - 1]
         index_a1++ until Array1[index_a1] == Array2[index_a2]
         index_a2 -=1 (these two lines are for the loop's sake in next iteration)
         index_a1 -=1

一言で言えば、上記の例では、

 Errors looks like this
 a,b,e

cとdが良いので。

4

3 に答える 3

3

数値インデックスの代わりに列挙子を使用して、2つの配列を反復処理できます。この例は、の対応する文字で始まる最初の単語をエコーし​​、次の重複をスキップしてa1、同時に繰り返します。a2a2a1a2

a1 = ["a", "b", "c", "d"]
a2 = ["apple", "angst", "banana", "clipper", "crazy", "dizzy"]

e2 = a2.each
a1.each do |letter|
  puts e2.next
  e2.next while e2.peek.start_with?(letter) rescue nil
end

a1(のすべての文字に少なくとも1つの単語が含まれa2、両方がソートされていることを前提としていますが、アイデアは得られます。)

于 2012-05-17T15:07:30.233 に答える
2

The for loop is not the best way to approach iterating over an array in Ruby. With the clarification of your question, I think you have a few possibly strategies.

You have two arrays, a and b. If both arrays are the same length:

a.each_index do |index|
 if a[index] == b[index]
   do something
 else
   do something else
 end
end

This also works if A is shorter than B.

If you don't know which one is shorter, you could write something like:

controlArray = a.length < b.length ? a : b to assign the controlArray, the use controlArray.each_index. Or you could use (0..[a.length, b.length].min).each{|index| ...} to accomplish the same thing.


Looking over your edit to your question, I think I can rephrase it like this: given an array with duplicates, how can I obtain a count of each item in each array and compare the counts? In your case, I think the easiest way to do that would be like this:

a = [:a,:a,:a,:b,:b,:c,:c,:d,:e,:e,:e]
b = [:a,:a,:b,:b,:b,:c,:c,:c,:d,:e,:e,:e]
not_alike = []
a.uniq.each{|value| not_alike << value if a.count(value) != b.count(value)}
not_alike

Running that code gives me [:a,:b,:c].

If it is possible that a does not contain every symbol, then you will need to have an array which just contains the symbols and use that instead of a.uniq, and another and statement in the conditional could deal with nil or 0 counts.

于 2012-05-17T17:20:47.683 に答える
0

the two arrays are praticatly the same except for a few elements that i have to skip in either/or every once in a while

Instead of skipping during iterating, could you pre-select the non-skippable ones?

a.select{ ... }.zip( b.select{ ... } ).each do |a1,b1|
  # a1 is an entry from a's subset
  # b1 is the paired entry bfrom b's subset
end
于 2012-05-17T17:23:28.977 に答える