[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Compare Array Values?

Darshan.Ishaya

11/23/2006 2:55:00 AM

Well, I'm new to Ruby, but it was an interesting question so I gave it
a stab. My first idea was

a.sort == b.sort

which works if all the elements of an array of of the same class. But
if you mix objects of different class (like you example with Fixnums
and a String), sort() craps out on it. You can pass a block to sort,
but I couldn't think of a good way to use that to our advantage. So I
resorted to more of a brute force method:

def same_elems?(array1, array2)
return false unless array1.length == array2.length

tmp = array2.clone
array1.each do |item|
if i = tmp.index(item) then tmp.delete_at(i) end
end
return tmp.empty?
end

As far as I can tell it meets all of your criteria.

Darshan

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?
>
> Cheers
> Daniel
>
> ------=_Part_45042_23178773.1164243892803--


2 Answers

Farrel Lifson

11/23/2006 5:38:00 AM

0

On 23/11/06, Darshan.Ishaya <Darshan.Ishaya@gmail.com> wrote:
> Well, I'm new to Ruby, but it was an interesting question so I gave it
> a stab. My first idea was
>
> a.sort == b.sort
>
> which works if all the elements of an array of of the same class. But
> if you mix objects of different class (like you example with Fixnums
> and a String), sort() craps out on it. You can pass a block to sort,
> but I couldn't think of a good way to use that to our advantage. So I
> resorted to more of a brute force method:

Would this work?
a.sort_by{|n| n.hash} == b.sort_by {|n|n.hash}

Farrel

Darshan.Ishaya

11/23/2006 6:18:00 AM

0

I think it does! I think hash() is the magic method that I was looking
for originally. I considered object_id(), but objects that are eql? can
have different object_id's, so it was a no-go. hash() seems to be the
magic one.

I think you get the award.

Darshan

Farrel Lifson wrote:
> On 23/11/06, Darshan.Ishaya <Darshan.Ishaya@gmail.com> wrote:
>> Well, I'm new to Ruby, but it was an interesting question so I gave it
>> a stab. My first idea was
>>
>> a.sort == b.sort
>>
>> which works if all the elements of an array of of the same class. But
>> if you mix objects of different class (like you example with Fixnums
>> and a String), sort() craps out on it. You can pass a block to sort,
>> but I couldn't think of a good way to use that to our advantage. So I
>> resorted to more of a brute force method:
>
> Would this work?
> a.sort_by{|n| n.hash} == b.sort_by {|n|n.hash}
>
> Farrel
>
>