简体   繁体   中英

compare 2 array contents in ruby

I have a 2 arrays from 2 different locations, but they contain the same elements. Below shows what I have:

@var2 =  ["2/05/2008", "$1.5000", "$2.0000"]
         ["1/06/2007", "$1.4000", "$1.0000"] 

@var3 =  ["1/06/2007", "$1.4000", "$1.0000"]
         ["2/05/2008", "$1.5000", "$2.0000"]

And this is the code how I arrive at those arrays above..not a good ruby code I acknowledge it

 var1 = Nokogiri::HTML(open(file.htm))
 var1.xpath('//tr[position() > 1]').map do |row|
 @var2 = row.xpath('td').map(&:text)[0,3]
    puts @var2
 end

File.open('some.txt') do |file|
  file.each_line do |line|
    line1 = line.split(' ')
    line2 = line1[0]
    line3 = "$" + line1[1]
    line4 = "$" + line1[2]

    @var3 = line2.split(' ') + line3.split(' ') + line4.split(' ')
    puts @var3
  end
  file.close()
end

In both of these variables I have multiple rows like this. How can I compare @var2 and @var3 ? Also @var2 is in descending order of date. Is there a way to compare the values/contents of the two and get a result? This also means that with the above format if I change a value in one of the arrays then the comparison should fail, for example:

@var2 =  ["2/05/2008", "$1.5000", "$2.0000"]
         ["1/06/2007", "$1.4000", "$1.0000"]

@var3 =  ["1/06/2007", "$1.4456", "$1.0000"]
         ["2/05/2008", "$1.5000", "$2.2222"]

I have tried several ways but unfortunately its not worked. I think I need to loop into the arrays and compare, but not sure how to do it.

I have tried @var2 & @var3 , @var2 - @var3 . They don't seem to work if I change a value in one of the arrays. The comparison still seems to check for the index and says matches is found. I want a exact row by row comparison with the values in each array.

Also further information : -

I want to know if the array contains the exact same elements, the order doesn't matter.

There can be duplicates within both the arrays var2 and var3. The array sizes will be the same. there won't be less elements nor NIL's.

Sort the arrays before you compare them:

@var2.sort == @var3.sort

Unless I'm missing something, this is basically what the problem is.

将它们的长度与它们的交叉长度进行比较:

@var2.length == @var3.length && @var2.length == (@var2 & @var3).length

Using - operator of Array class

2.1.1 :006 > arr1 = [1,2,3,4]
=> [1, 2, 3, 4]
2.1.1 :007 > arr2 = [4,3,2,1]
=> [4, 3, 2, 1]
2.1.1 :008 > arr1-arr2
=> []
2.1.1 :009 > 

So, you can do it like this

2.1.1 :001 > @var2 =  [["2/05/2008", "$1.5000", "$2.0000"],
2.1.1 :002 >              ["1/06/2007", "$1.4000", "$1.0000"] ]
 => [["2/05/2008", "$1.5000", "$2.0000"], ["1/06/2007", "$1.4000", "$1.0000"]] 
2.1.1 :003 > 
2.1.1 :004 >   @var3 =  [["1/06/2007", "$1.4000", "$1.0000"],
2.1.1 :005 >              ["2/05/2008", "$1.5000", "$2.0000"]]
 => [["1/06/2007", "$1.4000", "$1.0000"], ["2/05/2008", "$1.5000", "$2.0000"]] 
2.1.1 :006 > (@var2 - @var3) == []
 => true
2.1.1 :007 > 

怎么样

@var2.reverse == @var3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM