Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Search a table for a string
Message
De
21/07/2003 08:32:15
Mike Yearwood
Toronto, Ontario, Canada
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00811295
Message ID:
00811964
Vues:
55
Hiya Mark!

After looking at your code, I wondered why you used locate. Since there's no way to optimize substring searching, I guessed it would be quicker to do this...
LPARAMETERS tcString
IF VARTYPE(m.tcString) <> 'C'
   RETURN -1
ENDIF
LOCAL lnI, lnRec, lcString
lcString = LOWER(m.tcString)
lnRec = 0
SCAN REST WHILE m.lnRec = 0
  FOR lnI = 1 TO FCOUNT()  && assuming table is open in the current workarea
    IF INLIST(TYPE(FIELD(m.lnI)), 'C', 'M')
       IF m.lcString $ LOWER(EVALUATE(FIELD(m.lnI)))
         lnRec = RECNO()
         EXIT
       ENDIF
    ENDIF
  ENDFOR
ENDSCAN
IF m.lnRec#0
  GO m.lnRec
ENDIF
RETURN m.lnRec
Run once, there is little difference, but we never run things once! ;)

The non-locate code takes about 18 seconds for 1,000 searches. The locate code takes about 32 seconds. Coverage shows it is the locate command that is taking up the majority of the time.

>>Want to build a search function that would return the recno() of a row that contains a user defined string in any of the fields. Would like to make it generic so that I do not have to know the fields names. Can anyone point me in the right direction?
>>
>>Thanks
>>
>>Brenda
>
>As a start:
>
>lparameters lcString
>if vartype(lcString) <> 'C'
>   return -1
>endif
>local llFound, lnI, lnRec
>for lnI = 1 to fcount()  && assuming table is open in the current workarea
>    if inlist(type(field(lnI)), 'C', 'M')
>       lcField = FIELD(lnI)
>       locate for lower(lcString) $lower(&lcField)
>       if not eof()
>          llFound = .t.
>          exit
>       endif
>    endif
>endfor
>lnRec = 0
>if llFound
>   lnRec = RecNo()
>endif
>return lnRec
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform