2

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.

4
  • 2
    Half of your data given isn't in any variables, I assume they were meant to be nested arrays? Commented Mar 25, 2012 at 6:23
  • Also, your question is very unclear: do you want to know if the arrays contain the exact same elements, regardless of order? Or does order matter? Are all the elements unique? Commented Mar 25, 2012 at 6:29
  • @Andrew, yes sorry they are nested arrays. and want to know if the array contains the exact same elements, the order doesn't matter. Commented Mar 25, 2012 at 7:17
  • 3
    So edit your question to make them correct! Commented Mar 25, 2012 at 7:25

4 Answers 4

2

Sort the arrays before you compare them:

@var2.sort == @var3.sort

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Thanks for the response. I have updated the original question. Please have a look. Thanks
1

Compare their lengths with the length of their intersection:

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

4 Comments

Thanks for the edit @Marc-AndréLafortune! Been coding for far too many hours straight and it's far too late and I thus I end up making silly mistakes like that :).
Note that the solution assumes there are no duplicates.
Thanks Andrew. I think its going no where so here is the entire code and what I am doing. This aint great ruby code and I acknowledge it
var1 = Nokogiri::HTML(open('file.html)) var1.xpath('//tr[position() > 1]').map do |row| @var2 = row.xpath('td').map(&:text)[0,3] puts @var2.size end File.open('price.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.size end file.close() end
0

How about

@var2.reverse == @var3

8 Comments

Thanks, but this won't work mate. I want something where I'd be able to compare each and every value in the array "@var2 to @var3". So for example if I change a value in "@var3" from ["2/05/2008", "$1.5000", "$2.0000"] to ["2/05/2008", "$1.5000", "$2.22222"], then the comparison to "@var2" should be false and error out. Hope that helps
I want something where I'd be able to loop thru the values of the arrays
Did you try the code? It should compare every element of the array, and return false even if one thing doesn't match.
yep, and this is a smimple comparison I have
@user1126946 Can you post the code you used? That would clear things up.
|
0

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 > 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.