Página 1 de 1

Function to Compare two arrays ....

Publicado: Mar Jun 28, 2011 10:03 am
por ChrisGillard
Hi,
Am wanting to detect whether an array has changed from a clone of the
original array.
The arrays are multidimensional.
The == operator tests whether two array references point to the same array.
I want to detect any change in structure or contents between two separate
arrays.
Have looked through the docs but cant find anything, I was hoping not to
have to write something!
Any thoughts would be appreciated.
Chris Gillard

Function to Compare two arrays ....

Publicado: Mié Jun 29, 2011 2:01 pm
por ignacio
Chris Gillard escribió el mar, 28 junio 2011 10:03Hi,
Am wanting to detect whether an array has changed from a clone of the
original array.
The arrays are multidimensional.
The == operator tests whether two array references point to the same array.
I want to detect any change in structure or contents between two separate
arrays.
Have looked through the docs but cant find anything, I was hoping not to
have to write something!
Any thoughts would be appreciated.
Chris Gillard
Hi,
I believe there is no function for that purpose on Clipper or Harbour. Sorry.
regards,

Function to Compare two arrays ....

Publicado: Mié Jun 29, 2011 10:31 pm
por Carlos Ortiz
Chris te adjunto una funcion, creo que te puede servir.
a1 = { {111,222,333},{11,22},11 }
a2 = { {111,222,333},{11,22},11 }
ArrayCompare(a1,a2)
a1 = { {111,222,333},{11,22},11 }
a2 = { {111,222,333},{11,22},12 }
ArrayCompare(a1,a2)
a1 = { {111,222,333},{11,22},11 }
a2 = { {111,222,333},{11,23},11 }
ArrayCompare(a1,a2)
Saludo. Carlos Ortiz.
Argentina
************************
Function ArrayCompare(a1,a2)
************************
local lEqual,x,y
lEqual := Len(a1) == Len(a2)
if lEqual
for x = 1 to Len(a1)
if ValType(a1[x])=="A"
for y = 1 to Len(a1[x])
lEqual := a1[x,y] == a2[x,y]
if lEqual == .F. ; exit ; endif
? "a1:",a1[x,y], "a2:",a2[x,y] // test output
next y
else
lEqual := a1[x] == a2[x]
if lEqual == .F. ; exit ; endif
? "a1:",a1[x], "a2:",a2[x] // test output
endif
if lEqual == .F. ; exit ; endif
next x
endif
? lEqual // test status
return lEqual

Function to Compare two arrays ....

Publicado: Mar Jul 05, 2011 12:16 pm
por ChrisGillard
Thanks for the input both and to Carlos for some code that I have used and
works well.
Thank you.
Chris
"Carlos Ortiz" <cortizassist@hotmail.com> wrote in message
news:[email=4e0b8c60@svctag-j7w3v3j....]4e0b8c60@svctag-j7w3v3j....[/email]
> Chris te adjunto una funcion, creo que te puede servir.
>
> a1 = { {111,222,333},{11,22},11 }
> a2 = { {111,222,333},{11,22},11 }
> ArrayCompare(a1,a2)
> a1 = { {111,222,333},{11,22},11 }
> a2 = { {111,222,333},{11,22},12 }
> ArrayCompare(a1,a2)
> a1 = { {111,222,333},{11,22},11 }
> a2 = { {111,222,333},{11,23},11 }
> ArrayCompare(a1,a2)
>
> Saludo. Carlos Ortiz.
> Argentina
>
> ************************
> Function ArrayCompare(a1,a2)
> ************************
> local lEqual,x,y
> lEqual := Len(a1) == Len(a2)
>
> if lEqual
> for x = 1 to Len(a1)
> if ValType(a1[x])=="A"
> for y = 1 to Len(a1[x])
> lEqual := a1[x,y] == a2[x,y]
> if lEqual == .F. ; exit ; endif
> ? "a1:",a1[x,y], "a2:",a2[x,y] // test output
> next y
> else
> lEqual := a1[x] == a2[x]
> if lEqual == .F. ; exit ; endif
> ? "a1:",a1[x], "a2:",a2[x] // test output
> endif
> if lEqual == .F. ; exit ; endif
> next x
> endif
> ? lEqual // test status
>
> return lEqual
>
>