Comparing Arrays Function Needed 
Author Message
 Comparing Arrays Function Needed

Does anyone know where I can get a function which will compare
two arrays for equality?

I tried something like:

If aArray1 <> aArray2
   ? 'Arrays are different'
else
  ? 'Arrays are the same'
endif

And Clipper 5.2d and 5.2e do not like this...



Mon, 01 Jul 2002 03:00:00 GMT  
 Comparing Arrays Function Needed
Michael

How about

function cmpArrays( array1, array2 )

local lReturn := TRUE

if len( array1 ) != len( array2 )
    ? 'Array different sizes so can't be equal'
else
    for i := 1 to len( array1 )
        if array1[i] != array2[i]
            ? 'Array element ' + ntrim(i) + ' not equal'
            lReturn := FALSE
        endif
    next
endif
return lReturn

The above doesn't take into account elements of the array being an array
itself, I'll leave the recursive bit to you.

Quote:
> If aArray1 <> aArray2

This is because your only comparing memory locations not the array elements,
if both array1 & array2 were pointing to the same array then it would
elvaluate as false.
ie
array1 := {'array1'}
array2 := {'array2'}
x := array1
y := array2
? x == y     // .f.
? x[1]    // 'array1'
? y[1]    // 'array2'
y := array1
? x == y     // .t.
? x[1]    // 'array1'
? y[1]    // 'array1'

--
HTH
Steve Quinn
Are part-time band leaders semi-conductors?
Do jellyfish get gas from eating jellybeans?
How do you write zero in Roman numerals?
How many weeks are there in a light year?



Quote:
> Does anyone know where I can get a function which will compare
> two arrays for equality?

> I tried something like:

> If aArray1 <> aArray2
>    ? 'Arrays are different'
> else
>   ? 'Arrays are the same'
> endif

> And Clipper 5.2d and 5.2e do not like this...



Mon, 01 Jul 2002 03:00:00 GMT  
 Comparing Arrays Function Needed
Michael

{*filter*}, left something out.

function cmpArrays( array1, array2 )

local lReturn := TRUE

if len( array1 ) != len( array2 )
     ? 'Array different sizes so can't be equal'
     lReturn := FALSE
else
     for i := 1 to len( array1 )
         if array1[i] != array2[i]
             ? 'Array element ' + ntrim(i) + ' not equal'
             lReturn := FALSE
         endif
     next
endif
return lReturn

--
HTH
Steve Quinn
Are part-time band leaders semi-conductors?
Do jellyfish get gas from eating jellybeans?
How do you write zero in Roman numerals?
How many weeks are there in a light year?


Quote:
> Michael

> How about

> function cmpArrays( array1, array2 )

> local lReturn := TRUE

> if len( array1 ) != len( array2 )
>     ? 'Array different sizes so can't be equal'
> else
>     for i := 1 to len( array1 )
>         if array1[i] != array2[i]
>             ? 'Array element ' + ntrim(i) + ' not equal'
>             lReturn := FALSE
>         endif
>     next
> endif
> return lReturn

> The above doesn't take into account elements of the array being an array
> itself, I'll leave the recursive bit to you.

> > If aArray1 <> aArray2
> This is because your only comparing memory locations not the array
elements,
> if both array1 & array2 were pointing to the same array then it would
> elvaluate as false.
> ie
> array1 := {'array1'}
> array2 := {'array2'}
> x := array1
> y := array2
> ? x == y     // .f.
> ? x[1]    // 'array1'
> ? y[1]    // 'array2'
> y := array1
> ? x == y     // .t.
> ? x[1]    // 'array1'
> ? y[1]    // 'array1'

> --
> HTH
> Steve Quinn
> Are part-time band leaders semi-conductors?
> Do jellyfish get gas from eating jellybeans?
> How do you write zero in Roman numerals?
> How many weeks are there in a light year?



> > Does anyone know where I can get a function which will compare
> > two arrays for equality?

> > I tried something like:

> > If aArray1 <> aArray2
> >    ? 'Arrays are different'
> > else
> >   ? 'Arrays are the same'
> > endif

> > And Clipper 5.2d and 5.2e do not like this...



Mon, 01 Jul 2002 03:00:00 GMT  
 Comparing Arrays Function Needed

Quote:

> function cmpArrays( array1, array2 )

> local lReturn := TRUE

> if len( array1 ) != len( array2 )
>      ? 'Array different sizes so can't be equal'
>      lReturn := FALSE
> else
>      for i := 1 to len( array1 )
>          if array1[i] != array2[i]
>              ? 'Array element ' + ntrim(i) + ' not equal'
>              lReturn := FALSE
>          endif
>      next
> endif
> return lReturn

Or if it's important to know _where_ the arrays differ:

function cmpArrays( array1, array2 )

local lReturn := 0

if len( array1 ) != len( array2 )
     lReturn := min(len(array1),len(array2))+1
else
     for i := 1 to len( array1 )
         if array1[i] != array2[i]
             lReturn := i
             break
         endif
     next
endif
return lReturn

Quote:
> Do jellyfish get gas from eating jellybeans?

Yes, but it smells sweeter.

Quote:
> How do you write zero in Roman numerals?

You don't. Everybody ought to know that. Fscking Millennium debate.

Quote:
> How many weeks are there in a light year?

52-and-a-bit light weeks? But is a light year the same as a normal
year, but "Now With 30% Less Fat!"?

Richard



Mon, 01 Jul 2002 03:00:00 GMT  
 Comparing Arrays Function Needed
Yes I know how to compare KNOWN arrays (ie. known structure), but was
wondering
if anyone knew how to compare unknown arrays of varying elements,
subelement, or,
array of arrays, etc...


Quote:
> Michael

> How about

> function cmpArrays( array1, array2 )

> local lReturn := TRUE

> if len( array1 ) != len( array2 )
>     ? 'Array different sizes so can't be equal'
> else
>     for i := 1 to len( array1 )
>         if array1[i] != array2[i]
>             ? 'Array element ' + ntrim(i) + ' not equal'
>             lReturn := FALSE
>         endif
>     next
> endif
> return lReturn

> The above doesn't take into account elements of the array being an array
> itself, I'll leave the recursive bit to you.

> > If aArray1 <> aArray2
> This is because your only comparing memory locations not the array
elements,
> if both array1 & array2 were pointing to the same array then it would
> elvaluate as false.
> ie
> array1 := {'array1'}
> array2 := {'array2'}
> x := array1
> y := array2
> ? x == y     // .f.
> ? x[1]    // 'array1'
> ? y[1]    // 'array2'
> y := array1
> ? x == y     // .t.
> ? x[1]    // 'array1'
> ? y[1]    // 'array1'

> --
> HTH
> Steve Quinn
> Are part-time band leaders semi-conductors?
> Do jellyfish get gas from eating jellybeans?
> How do you write zero in Roman numerals?
> How many weeks are there in a light year?



> > Does anyone know where I can get a function which will compare
> > two arrays for equality?

> > I tried something like:

> > If aArray1 <> aArray2
> >    ? 'Arrays are different'
> > else
> >   ? 'Arrays are the same'
> > endif

> > And Clipper 5.2d and 5.2e do not like this...



Tue, 02 Jul 2002 03:00:00 GMT  
 Comparing Arrays Function Needed



Quote:
> Yes I know how to compare KNOWN arrays (ie. known structure), but was
> wondering
> if anyone knew how to compare unknown arrays of varying elements,
> subelement, or,
> array of arrays, etc...

Very simple:

Function cmpArrays(arr1,arr2)
   local lRet:=.t.
   Begin Sequence
     if ValType(arr1) == "A" .and. ValType(arr2) == "A"

     else
        lRet:=.f.
     endif
   end
   return lRet

static procedure __cmpTwoArrays(arr1, arr2, lRet)
   local i
   local nLen:=Len(arr1)
   if nLen <> Len(arr2)
      lRet:= .f.
      break
   elseif nLen == 0
      return
   else
     for i := 1 to nLen
         if ValType(arr1[i]) == "A" .and. ValType(arr2[i]) == "A"

         elseif arr1[i] != arr2[i]
            lRet := .f.
            break
         endif
     next
   endif
   return



Tue, 02 Jul 2002 03:00:00 GMT  
 Comparing Arrays Function Needed


[SNIP]

Quote:
>          elseif arr1[i] != arr2[i]

That code will blow if arr1[i] and arr2[i] are different data types.

Here is a function that should work (although I just typed it into my
browser & may miss a syntax error or two):

Function ArrayCompare(a1, a2)
  local lSame := .T.
  local x

  if valtype(a1) <> "A" .or. valtype(a2) <> "A" // Not arrays?
    lSame := .F.
  elseif len(a1) <> len(a2) // Different lengths?
    lSame := .F.
  else
    for x := 1 to len(a1)
      if valtype(a1[x]) == valtype(a2[x]) // Same data type?
        if valtype(a1[x]) == "A"
          lSame := ArrayCompare(a1[x], a2[x]) // Recurse subarrays
        else
          lSame := (a1[x] == a2[x]) // Exactly equal values?
        endif
      else
        lSame := .F.
      endif
      if !lSame // Don't continue once failure is known!
        exit
      endif
    next
  endif

Return(lSame)

--
Ray Marron

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 02 Jul 2002 03:00:00 GMT  
 Comparing Arrays Function Needed
Cool, thanks...  I will give it a shot.....

I forgot about recursion.....


Quote:


grup

> > Yes I know how to compare KNOWN arrays (ie. known structure), but was
> > wondering
> > if anyone knew how to compare unknown arrays of varying elements,
> > subelement, or,
> > array of arrays, etc...

> Very simple:

> Function cmpArrays(arr1,arr2)
>    local lRet:=.t.
>    Begin Sequence
>      if ValType(arr1) == "A" .and. ValType(arr2) == "A"

>      else
>         lRet:=.f.
>      endif
>    end
>    return lRet

> static procedure __cmpTwoArrays(arr1, arr2, lRet)
>    local i
>    local nLen:=Len(arr1)
>    if nLen <> Len(arr2)
>       lRet:= .f.
>       break
>    elseif nLen == 0
>       return
>    else
>      for i := 1 to nLen
>          if ValType(arr1[i]) == "A" .and. ValType(arr2[i]) == "A"

>          elseif arr1[i] != arr2[i]
>             lRet := .f.
>             break
>          endif
>      next
>    endif
>    return



Wed, 03 Jul 2002 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Comparing arrays to array aggregates

2. compare function in sort function of lists

3. Passing array valued functions as argument to function.

4. F95: array-returning function as argument to another function

5. compare array containing strings with numbers

6. Compare 2 multidimensional arrays

7. comparing two arrays with same length

8. Comparing data from an array

9. Multi dimensional arrays (awk compared to others)

10. Comparing two arrays

11. How to compare arrays?

12. Comparing Two or More Arrays

 

 
Powered by phpBB® Forum Software