469,314 Members | 2,129 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,314 developers. It's quick & easy.

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 '08 #1
3 5940
improvcornartist
303 Expert 100+
You can use 'include?' as necessary to determine if the array meets the requirements.
Expand|Select|Wrap|Line Numbers
  1. if array.include?(1)
  2. #do this
Feb 8 '08 #2
But is there any way to check for multiple values? I realize that I could nest it, but that would be extremely inconvenient.
Feb 8 '08 #3
improvcornartist
303 Expert 100+
I'm not sure how your array is set up, so I can't really give you a specific answer, but you should be able to use set intersection or array difference.
Expand|Select|Wrap|Line Numbers
  1. #Set Intersection
  2. if array && [1,2,3] == [1,2,3]
  3.   #array contains 1,2,3, so do this
  4. end
  5.  
Feb 8 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Peter | last post: by
1 post views Thread by Iain | last post: by
12 posts views Thread by Elijah Bailey | last post: by
4 posts views Thread by agent349 | last post: by
1 post views Thread by Donald Grove | last post: by
4 posts views Thread by eoghan.kenny | last post: by
19 posts views Thread by Ole Nielsby | last post: by
11 posts views Thread by Sheldon | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.