SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    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.

  2. #2
    SitePoint Evangelist
    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

  3. #3
    SitePoint Enthusiast AllTom's Avatar
    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.

  4. #4
    SitePoint Guru
    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

  5. #5
    SitePoint Evangelist
    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.

  6. #6
    SitePoint Enthusiast AllTom's Avatar
    Join Date
    Feb 2008
    Location
    Wherever danger lies...
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    Code ruby:
    def approve?(ary, *arys)
      arys.any?{|x| ary & x == x}
    end
    I kind of like this one, too. It'd be a bit unnatural for me, and took me a moment to "get," but it's short and sweet.

  7. #7
    SitePoint Guru
    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
    Attached Images Attached Images

  8. #8
    SitePoint Evangelist
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •