mhz
mhz

Reputation: 1029

Two indexes in Ruby for loop

can you have a ruby for loop that has two indexes? ie:

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

Can't find anything in google

EDIT: Adding in more details

I need to compare two different arrays like such

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

But knowing that they both have the same items (abcde) This is my logic in pseudo, lets assume this whole thing is inside a loop

#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

In a nutshell, in the example above,

 Errors looks like this
 a,b,e

As c and d are good.

Upvotes: 1

Views: 1098

Answers (3)

philosodad
philosodad

Reputation: 1808

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.

Upvotes: 2

Phrogz
Phrogz

Reputation: 303254

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

Upvotes: 0

Rob Davis
Rob Davis

Reputation: 15772

You could iterate over two arrays using Enumerators instead of numerical indices. This example iterates over a1 and a2 simultaneously, echoing the first word in a2 that starts with the corresponding letter in a1, skipping duplicates in a2:

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

(It assumes all letters in a1 have at least one word in a2 and that both are sorted -- but you get the idea.)

Upvotes: 3

Related Questions