SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Comparing Arrays
-
Feb 7, 2008, 21:30 #1
- Join Date
- Feb 2008
- Posts
- 1
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Comparing Arrays
I'm a Ruby n00b trying to write a simple app, and I've been trying to find a way to do this for a while, but can't seem to find anything. Here is essentially what I want to do:
if array contains 1,2,3 or 4,5,6 then
execute this code
But the array may have many more numbers in it than that, and they won't be in the same order. All I've been able to find are things where the two arrays have to be exactly alike.
-
Feb 8, 2008, 07:20 #2
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How about this for a starter:
Code:elements_not_found = [1, 2, 3].delete_if {|x| array_to_test.include?(x)} if elements_not_found.length == 0 code_to_execute else puts "The elements not found were: #{elements_not_found.join(', ')}" end
Last edited by ReggieB; Feb 8, 2008 at 07:35. Reason: Needs to test equality
-
Feb 8, 2008, 08:47 #3
- Join Date
- Feb 2008
- Location
- Wherever danger lies...
- Posts
- 48
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can write it intuitively when you are familiar with some of the more obscure methods of the Enumerable module (my favorite module!). See the documentation for Enumerable, especially the methods #any? and #all?.
To test whether your array includes all of a set of numbers:
Code Ruby:[1, 2, 3].all? { |num| ary.include? num }
You want to test against two arrays, so you'd probably pull it out as a function. The function might take the array to test, and arrays of approved sets. The function will test if the given array includes all of any set of numbers:
Code Ruby:def approve?(ary, *approved_sets) approved_sets.any? do |approved_set| approved_set.all? { |num| ary.include? num } end end approve? some_ary, [1, 2, 3], [4, 5, 6]
You would name this function something more appropriate to your specific use case because it is not very general. I cannot tell what you would use it for, so I chose a fairly poor function name.
-
Feb 8, 2008, 13:31 #4
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code ruby:def approve?(ary, *arys) arys.any?{|x| ary & x == x} end
-
Feb 10, 2008, 05:30 #5
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I thought there'd be a better way to do it
Using Enumerable methods is a much better solution than my delete_if one.
-
Feb 10, 2008, 08:18 #6
-
Feb 11, 2008, 13:32 #7
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It's easier if you look at it visually.
The big bubble is ary and the small one is x. The overlapping area is "ary & x": the elements that are in ary and in x. So if x is contained inside ary, then the overlapping area is equal to x (as you can see in the second picture).
We can write a contains? function like this:
Code ruby:def contains?(ary, x) overlapping_area = ary & x return overlapping_area == x end
I hope this makes sense. Now you should be able to understand this if you look at the pictures:
Code ruby:def contains?(ary, x) areas_combined = ary | x return areas_combined == ary end
-
Feb 12, 2008, 04:43 #8
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Splendid explanation Fenrir2. It makes it clearer to me how your solution works. Very neat.
Bookmarks