Hi thomas,
>>I really love those challenges. Though I also have one that uses recursion, the following seems to be most effective (And readable).
>>
>I won't argue readability here, since it is to a large account a
>matter of personal preference and self-training <g>.
>The "problem" with this approach is, that on increasingly large array's
>the first case will possibly be evaluated quite often to being untrue.
In theory, yes, the order in which the CASE statements are evaluated does matter, however In practise it seem neglible. However, You're right it should be on the bottom
>In vfp I try to order the cases according to expected frequency, and that
>puts this case last. Also there is an added check in the do while...
>"My" favorite (including softseek, but that could be included here as well)
>is in the answer to George.
How about this one, which takes a bit different appoach and theorectically does an extra loop on the average, but is far more efficient (Almost twice as fast as my former solution), But is also less readable as it is difficult to see what it exactly does do.
FUNCTION SearchArray(aArray, cSearchValue, lSoftSeek)
LOCAL nIndex, nLen, nT
nLen = ALEN(aArray,1)
nIndex = 0
FOR nT = INT(LOG(nLen)/LOG(2)) TO 0 STEP -1
IF BITSET(nIndex,nT) =< nLen AND aArray[BITSET(nIndex,nT)] =< cSearchValue
nIndex = BITSET(nIndex, nT)
ENDIF
ENDFOR
RETURN IIF(nIndex > 0 AND aArray[nIndex] == cSearchValue, nIndex, IIF(lSoftSeek, nIndex+1 % nLen+1,0))
Regards,
Walter,