On Thu, 23 Nov 2006, Daniel N wrote: > I want to check to see if two arrays contain the same values. > That is, are they the same array but not in the same order. > > Example > equal > [1,2,3,4,"abc"] should equal [2,3,1,"abc",4] and all the other different > orders that are possible > > This should not be equal > [1,2,3] and [1,1,2,3] > [1,2,3,nil] and [nil, nil, nil, 1,2,1,3,2] > > At first I thought of > (( array1 | array2 ) - array1).size == 0 > > but this does not take into account duplicate values and the should not > equals do equal :(. In fact I have not been able work out how to use any of > the standard array, or Enumerable methods in such a way I can check if two > arrays contain the same values as well as taking into account duplicates. > > Any pointers? maybe i mis-undestand, but why wouldn't you use a hashlist? harp:~ > cat a.rb def content_eq a, b ah = Hash.new{|h,k| h[k] = []} and a.each{|x| ah[x] << x} bh = Hash.new{|h,k| h[k] = []} and b.each{|x| bh[x] << x} ah == bh end pairs = [ [ [1,2,3,4,"abc"], [2,3,1,"abc",4] ], [ [1,2,3], [1,1,2,3] ], [ [1,2,3,nil], [nil, nil, nil, 1,2,1,3,2] ], ] pairs.each{|a,b| puts "content_eq(#{ a.inspect }, #{ b.inspect }) #=> #{ content_eq a, b }"} harp:~ > ruby a.rb content_eq([1, 2, 3, 4, "abc"], [2, 3, 1, "abc", 4]) #=> true content_eq([1, 2, 3], [1, 1, 2, 3]) #=> false content_eq([1, 2, 3, nil], [nil, nil, nil, 1, 2, 1, 3, 2]) #=> false ?? -a -- my religion is very simple. my religion is kindness. -- the dalai lama