# Array Comparison

Hello everyone. First of all, sorry for my english.

I would like to compare the content between two arrays.

To make it easier to understand what I want, I will type an example.

I am using two arrays.

The first one contains [1,2,3] and is a kind of constant, it remains the
same.

The second one is variabe and keeps changing. It may be, for example,
[4,8,9,1,2,3,5]

What I want to do is to compare the two arrays in order to check if the
second array contains the content of the first array, ordered in the
same way.
As you can see, my second array contains 1,2,3, but what method can I
use to check if the second array contains the same numbers as the first
array with the same order.

To make it easier I would like to check if the array [4,8,9,1,2,3,5]
contains the array [1,2,3], but the array.include?([1,2,3]) command
seems not to work.

Guy Fuilo wrote in post #1003797:

To make it easier I would like to check if the array [4,8,9,1,2,3,5]
contains the array [1,2,3], but the array.include?([1,2,3]) command
seems not to work.

No, that would detect if [1,2,3] is an element of the array, e.g.
[4,8,9,[1,2,3],5]

For that example I would suggest:

array = [4,8,9,1,2,3,5]
array.each_cons(3).find { |x| x == [1,2,3] }

Have a look (in irb) at

array.each_cons(3).to_a

to see what this does.

HTH,

Brian.

Unfortunately my library does not contain each_cons nor find for the
Array class.

Since I am using an in-game ruby Editor I can not change its library.

Maybe you have any idea how to do it in an other way ?

Here are the only methods my Array class contains :
http://k-du.de/rgss/sc_array.html

Can’t edit…

Well there is a problem.

[5,4,1,2,3].check?([1,2,3]) is working (return true)
[5,4,1,2,4,3].check?([1,2,3]) is working (return false)
[2,1,3].check?([1,2,3]) is NOT working. returns true while it should
return false.

Could anyone help me correcting the code I posted earlier ?

Guy Fuilo wrote in post #1003804:

Unfortunately my library does not contain each_cons nor find for the
Array class.

target = [1,2,3]
data = [
[5,4,1,2,3],
[5,4,1,2,4,3],
[2,1,3],
]

target_str = target.join

data.each do |arr|
data_str = arr.join

if data_str.match(target_str)
puts ‘yes’
else
puts ‘no’
end
end

–output:–
yes
no
no

I tried something but I am not an expert so I would appreciate if anyone
could ensure that this will work properly.

I added a method to the Array class :

class Array
def check?(array)
count = 0
done = []
stop = false
for value in self
for i in 0…array.size
if not stop
count = 0 if value != array[i] and not done.include?(i)
next if not value == array[i]
count += 1
done.push(i)
stop = true
end
end
stop = false
end
return true if count == array.size
return false
end
end

Then I use the method to compare my two arrays, for example:
array1 = [4,5,1,2,3]
array2 = [1,2,3]
array1.check?(array2)

Is that correct ?

my code is this:

array = [4,8,9,1,2,3,5]
check_array = [1,2,3]

array.to_s.include?(check_array.to_s)
=> true

is ok?

Guy Fuilo wrote in post #1003821:

Could anyone help me correcting the code I posted earlier ?

It’s too convoluted. Try this:

class Array
def check?(array)
self.length.times do |i|
if self[i] == array.first #or array[0]
if self[i, array.length] == array
return true
end
end
end

``````return false
``````

end

end

target = [1,2,3]
data = [
[5,4,1,2,3],
[5,4,1,2,4,3],
[2,1,3],
]

data.each do |arr|
puts arr.check?(target)
end

–output:–
true
false
false

Thank you chucai this code is perfect and exactly what I needed.

bill gate wrote in post #1003844:

my code is this:

array = [4,8,9,1,2,3,5]
check_array = [1,2,3]

array.to_s.include?(check_array.to_s)
=> true

is ok?

Yes, very nice.

Well the maximum size of my arrays is seven values.
And each value is only 1, 2 or 3.

It is a system made for combos attacks. If you proceed 1,2,3, then a
combo is unleashed.

In that case it seems to work properly with chucai’s method.

bill gate wrote in post #1003844:

my code is this:

array = [4,8,9,1,2,3,5]
check_array = [1,2,3]

array.to_s.include?(check_array.to_s)
=> true

is ok?

No, I think this is badly broken. The OP is presumably using ruby 1.8.6
or earlier, because he doesn’t have each_cons.

In that case, to_s just joins the elements together without any
separator:

irb(main):001:0> array = [4,8,9,1,2,3,5]
=> [4, 8, 9, 1, 2, 3, 5]
irb(main):002:0> array.to_s
=> “4891235”

Clearly, searching for “123” within this works properly only for the
special case where all the array elements are single digits.

array = [41,82,93,1,2,3,54]
check_array = [12,3]
array.to_s.include?(check_array.to_s)
=> true ?!

If you want to do a string match, it should be something like this:

array = [4,8,9,1,2,3,5]
check_array = [1,2,3]
outer = “,#{array.join(”,")},"
inner = “,#{check_array.join(”,")},"
puts outer.include? inner

Clearly, this also works only for objects which implement a meaningful
to_s method, and where the to_s value doesn’t include a comma.

If you are doing this without each_cons I would suggest the following:

class Array
def include_subarray?(other)
(size - other.size + 1).times do |i|
return true if self[i,other.size] == other
end
return false
end
end
array = [4,8,9,1,2,3,5]
check_array = [1,2,3]
array.include_subarray?(check_array)

But in any case it’s probably worth upgrading to ruby 1.8.7, since a lot
of libraries now depend on features in it.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.