472,131 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 software developers and data experts.

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 6009
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
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
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 leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.