[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?

Morton Goldberg

11/23/2006 2:23:00 AM

On Nov 22, 2006, at 8:04 PM, 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]

In general, if the arrays can contain arbitrary elements, this is a
difficult problem. If the elements themselves can be arrays, you will
want to start by duplicating the two arrays and flattening the
duplicates. If the duplicates are not the same size, you're done. If
they are the same size, you will need to define a notion of canonical
order (basically a sort order that is applicable to all elements) and
transform both arrays into canonical order -- this can be the hard
part. Then you can make an element by element comparison.

Regards, Morton

1 Answer

dblack

11/23/2006 2:31:00 AM

0