Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Is ListBox Okay?
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00272435
Message ID:
00272457
Vues:
27
>Hello George!
>>Have you tried something like this:
>>FOR lni = 1 TO ThisList.ListCount
>> IF ThisList.List(lni) = "whatever"
>> EXIT
>> ENDIF
>>NEXT
>No but I will right now!
>>
>>And if so what were the results. BTW, if the list is ordered there are much faster ways of doing this search.
>SORTED is True: Does that mean its ordered?

It should be

>PS Have you noticed SORTED doesn't bite unless he is set true from an init event? Simply setting SORTED True from the property tool is not enough!

No, I haven't noted that. I suspect it's because the lists I search are ordered by the query or whatever method I use to populate the list.

>So whats a faster way, if SORTED = True?

Sure, the code below implements a binary search on an ordered list.
LPARAMETERS poList, pSearchFor, plSoft

LOCAL lnresult, lnfirst, lnlast, lnmid,;
  llfound, llsoft
llfound = .F.
lnresult = 0
lnfirst = 1
lnlast = poList.ListCount
IF PCOUNT() = 3
  llsoft = plSoft
ELSE
  llsoft = .F.
ENDIF
DO WHILE lnfirst <= lnlast AND NOT llfound
  lnmid = INT((lnfirst + lnlast) / 2)
  llfound = (pSearchFor == poList.List(lnmid))
  IF NOT llfound
    IF pSearchFor > poList.List(lnmid)
      lnfirst = lnmid + 1
    ELSE
      lnlast = lnmid - 1
    ENDIF
  ENDIF
ENDDO
IF llfound
  lnresult = lnmid
ELSE
  IF llsoft
    lnresult = lnfirst
  ENDIF
ENDIF
RETURN lnresult
If the return value is 0, a match wasn't found. Passing .T. as the last parameter does what's known as a "soft search". In that case, the value of the next greatest element is returned.
George

Ubi caritas et amor, deus ibi est
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform