Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
What is the best way to compare two ARRAYS?
Message
From
10/12/1997 15:51:55
 
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00064972
Message ID:
00065114
Views:
33
>>I want to compare two arrays to see if one
>>is different. What is the best way?
>
>Try the following in a function. llresult will return .T. if the arrays are identical.
>
>LPARAMETERS parray1, parray2
>
>LOCAL llresult, lnlast1, lnlast2, lni
>lnlast1 = ALEN(parray1)
>lnlast2 = ALEN(parray2)
>lni = 0
>llresult = (lnlast1 = lnlast2)
>DO WHILE llresult AND lni < lnlast
> lni = lni + 1
> llresult = (parray1[lni] = parray2[lni])
>ENDDO
>RETURN llresult
>
>hth,
>
>George


I would change it slightly to improve performance by letting vfp do the incrementing and letting the user select starting the compare loop at the bottom if that is where the difference is most likely to occur (need speed!!):

LPARAMETERS parray1, parray2, top_bottom

if parameters() < 3
top_bottom = 0 && start at top (1)
endif

LOCAL lnlast1, lnlast2, lni

lnlast1 = ALEN(parray1)
lnlast2 = ALEN(parray2)

do case

case (lnlast1 != lnlast2)
return .f.

case top_bottom != 0

for lni = lnLast1 to 1 step -1
if (parray1[lni] != parray2[lni])
return .f.
endif
endfor

otherwise

for lni = 1 to lnLast1
if (parray1[lni] != parray2[lni])
return .f.
endif
endfor

endcase

RETURN .t.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform