Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
What is the best way to compare two ARRAYS?
Message
De
10/12/1997 15:51:55
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00064972
Message ID:
00065114
Vues:
38
>>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.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform