Syntax error in IF with string expressions

Started by Stéphane Devouard, March 13, 2023, 04:28:01 AM

Previous topic - Next topic

Stéphane Devouard

Hi
My boss found the issues in the attached image while doing some code

I've tried some variations with parentheses to transform variables into values but still getting the error 20
Stéphane Devouard
Portfolio | Work

Ken Sproul

Hi Stephane,


I found that the "if string$ " syntax only works when the result of the expression is a string. The manual could be clearer with examples, but it says the if statement can have either numeric or string expressions.  It doesn't say that it cannot have both, but that is implied.  We have to think of and's and or's as part of a numeric expression because they result in a Boolean value, which ultimately is numeric.


The way to test this is whether the expression can be used in a let statement with either a string or numeric variable.
These don't work because they mix strings and numerics:
x$=a$ and 1
x=a$ and 1
These work because they don't mix variable types:
x$=a$
x=not(nul(a$)) and 1
x=not(nul(a$)) and not(nul(b$))


Hope it makes more sense now.

Ken Sproul
DPI Information Service, Inc.
Pivotal Systems LLC

James Zukowski

Like with Ken, I've found that most mixing of a simple string$ with anything in a boolean-type test will generate an Error 20. What I've done is to essentially transform it all to numeric/boolean testing, like not(nul(X$)), which would work in all boolean cases.

BTW, Ken, "if X$=A$ and 1" is a viable test, as the X$=A$ yields a boolean result.
James Zukowski
Sr. Developer - J&E

BRAND>SAFWAY
Brand Industrial Services

Ken Sproul

Should have included these in my previous reply.


To do "a$ or b$":


if a$+b$ then ...


To do "a$ and b$":
if a$ then if b$ then ...


Hi James,


The "test" I'm referring to is with a "let" statement, I just omitted the "let":
let x$=a$ and 1
etc.

Ken Sproul
DPI Information Service, Inc.
Pivotal Systems LLC

Stéphane Devouard

I usually use nul(string$) or not(nul(string$))

I usually avoid complex conditions with nested IF ELSE and { }

Even in languages that have fancy IDEs with indentation and code folding, developers recommend using "guards"
ie simple IFs to prematurely exit from a given process or function

I often use

for (some condition)
! // do the work
next



Or if there are more conditions to test

for (1)
if (some condition) then break
if (some other condition) then break
! // lets do the work
next


Anyway, thanks guys for your input
Stéphane Devouard
Portfolio | Work