PxPlus User Forum

Twitter Twitter Twitter

Author Topic: Syntax error in IF with string expressions  (Read 619 times)

Stéphane Devouard

  • Diamond Member
  • *****
  • Posts: 122
  • PxPlus guru with skills in PHP, JS, C#, Java
    • View Profile
    • Stéphane's Web Resume
Syntax error in IF with string expressions
« on: March 13, 2023, 04:28:01 AM »
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

  • Gold Member
  • ****
  • Posts: 60
    • View Profile
Re: Syntax error in IF with string expressions
« Reply #1 on: March 13, 2023, 08:32:56 AM »
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

  • Diamond Member
  • *****
  • Posts: 297
    • View Profile
Re: Syntax error in IF with string expressions
« Reply #2 on: March 13, 2023, 08:44:14 AM »
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

  • Gold Member
  • ****
  • Posts: 60
    • View Profile
Re: Syntax error in IF with string expressions
« Reply #3 on: March 13, 2023, 08:47:49 AM »
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

  • Diamond Member
  • *****
  • Posts: 122
  • PxPlus guru with skills in PHP, JS, C#, Java
    • View Profile
    • Stéphane's Web Resume
Re: Syntax error in IF with string expressions
« Reply #4 on: March 13, 2023, 12:26:07 PM »
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
Code: [Select]
for (some condition)
! // do the work
next


Or if there are more conditions to test
Code: [Select]
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