English 中文(简体)
鲁比的两种 index指数
原标题: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: 在更多细节中添加

我需要比较两种不同的阵列,如这样的阵列。

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

如上所示,在营养中,

 Errors looks like this
 a,b,e

情况良好。

最佳回答

诉讼不是处理在鲁比拉的一个阵列问题的最佳途径。 在澄清你的问题之后,我认为你有一些可能的战略。

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

如果A少于B,则这项工作也将进行。

如果你不知道什么是短的,你可以写一些这样的东西:

Control Array = a.length < b.length ? a: b tosign the controlArray, the use control Array.each_index. 或您可使用<代码>(0. [a.length, b.length,min].each{><>>>>><<>/code完成同样的事情。


看看你对你的怀疑,我认为我可以这样说:鉴于存在重复的阵列,我如何能够从各个阵列获得每个项目的分数,并比较各项内容? 就你而言,我认为这样做最容易的办法是:

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].

如果不可能包含每个编号,那么,你就需要有一阵列,其中仅包括符号和使用,而不是一个.uniq,而另一个<代码>和的有条件发言可以处理零或0项。

问题回答

你们可以使用计算器而不是数字指数,使两个阵列多变。 这一实例在<代码>a1和a2上公布。 同时,在<代码>a2中加入第1个字,从<代码>a1的相应字母开始,在<代码>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

(在<代码>a1上假设所有信件。) <代码>a2至少有一字,两者均分门别类——但大家有这个想法。

两种阵列实际上相同,但一些因素除外,这些要素在一栏内必须每秒钟一次。

难道你在开斋节时不要ski笑,而是会预先选取无法使用的人吗?

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




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签