PDA

View Full Version : Support IN for lists of strings



Robert Hodge
18-06-2013, 17:31
Presently, the IN function is defined as

n = In(ExpressionToCheck, Value1 [, Value2 [, Value...]])

where each expression is supposed to be numeric. The ExpressionToCheck is compared against the list of values, and if a match is found, an index to the matched value is returned, otherwise 0 if no match is found.

However, if you use strings as values, the function unconditionally returns 1.

It would be nice if this worked for strings.

It would be even nicer if there were some way to 'adjust' the comparison test, so you could say it was case insensitive, or ignored leading and trailing blanks, etc.

ErosOlmi
30-06-2013, 19:25
In reality there is an undocumented IN$ function (undocumented because I forgot document it):
n = In$(ExpressionToCheck, Value1 [, Value2 [, Value...]])

It has not TRIM or CASE options.

Robert Hodge
30-06-2013, 20:07
Thanks, IN$ does just what you'd expect it to.

I was thinking that an array version of IN and IN$ would be useful. After doing some searching, I came up with a pair of functions that would do the job. I call these HAS and HAS$ for lack of better names: It would be pretty simple to add UCASE$ and/or TRIM$ to the HAS$ function. Example:


DIM A(3) AS STRING = "ONE", "TWO", "THREE"
DIM N AS NUMBER

N = HAS$("TWO", A)
MSGBOX 0, "TWO FOUND IN A(" & TSTR$(N) & ")"
STOP

FUNCTION HAS$ (X AS STRING, A() AS STRING) AS NUMBER
DIM N AS NUMBER
N = ARRAY SCAN A(), = X
RETURN N
END FUNCTION

FUNCTION HAS (X AS NUMBER, A() AS NUMBER) AS NUMBER
DIM N AS NUMBER
N = ARRAY SCAN A(), = X
RETURN N
END FUNCTION