Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7955661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:45:52+00:00 2026-06-04T03:45:52+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:45:53+00:00Added an answer on June 4, 2026 at 3:45 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of Ruby Date objects. How can I find the newest
I am working within a Ruby on Rails app. I have a loop that
I have a ruby loop that creates a list of comments.. I wonder if
I have a Ruby on Rails app running on my server, and I can't
I have a ruby hash which looks like {10.1.1.6=>nick, 127.0.0.1=>nick1} But I can't manage
I have a phone number field that can have lots of different characters such
I have an element that can have multiple types (not my design). The element
I have noticed in Java that you can have a function with object... as
I have a class that I can have many instances of. Inside it creates
i have some Orders that can have several Items and these Items have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.