evaluate a string

Posted by ecsousa on 26-Feb-2015 15:45

I have a variable string containing a logic expression. 
I want a way to evaluate this variable to find out the result of the expression.  Has any function that allows me to do this?

Example:
Define variable cExpression As Character No-undo.

Assign cExpression = "true or false" .

I´d like to get the value of the logical expression...is it possible? 

Thank you,


Éllen

All Replies

Posted by Laura Stern on 26-Feb-2015 15:51

I don't really get your example of "true or false".  That is kind of meaningless.

I assume you mean something more like one of the following:

cExpression = "myInt > 5".  

cExpression = "myInt = 5 OR mycc = "ABC".

No - there is nothing in the language that will take a logical expression as a string and evaluate it.

Posted by Stefan Drissen on 26-Feb-2015 16:11
Posted by Mike Fechner on 26-Feb-2015 16:23

For certain expressions you can use a single record temp-table and the dynamic find. This evaluates the second expression from Lauras post.
 
 
DEFINE TEMP-TABLE ttEval NO-UNDO
   FIELD myInt AS INTEGER
   FIELD mycc AS CHARACTER
   .
 
DEFINE VARIABLE cExpression AS CHARACTER NO-UNDO.
 
/* ***************************  Main Block  *************************** */
 
CREATE ttEval.
ASSIGN myInt = 6
       mycc = "ABC" .
      
ASSIGN cExpression = "myInt = 5 OR mycc = ~"ABC~"".
 
BUFFER ttEval:FIND-FIRST ("WHERE " + cExpression) NO-ERROR .
 
MESSAGE "true?" AVAILABLE ttEval
    VIEW-AS ALERT-BOX.
 
 

Posted by Richard.Kelters on 26-Feb-2015 16:52

Create a dummy temp-table with 1 record.

Create a query dynamically on this temp table.

Parse your expression in the prepare string of the query.

Something like "for each tt where true or false" or "for each tt where 3 + 2 > 5".

In the latter you can you can replace the 2 with the value of a variable for example substitue("for each tt where 3 + &1 > 5", variable) and suddenly you have run time evaluation.

Check availbilty of tt record or query result, to see if evaluation is true.

That's in short the knowledgebase entry. Took me some time to crasp.

Posted by Brian K. Maher on 27-Feb-2015 05:23

 
Can't you just use the LOGICAL() function?
 

Posted by Brian K. Maher on 27-Feb-2015 05:29

 
Nevermind, I misread the doc (too early in the morning).
 
 

Posted by Mike Fechner on 27-Feb-2015 05:29

LOGICAL ("FALSE AND TRUE") returns FALSE.
 
So I guess, logical just converts the first word to true or false from yes/no/true/false.
 
 
Von: Brian K. Maher [mailto:bounce-maher@community.progress.com]
Gesendet: Freitag, 27. Februar 2015 12:24
An: TU.OE.Development@community.progress.com
Betreff: RE: [Technical Users - OE Development] evaluate a string
 
Reply by Brian K. Maher
 
Can't you just use the LOGICAL() function?
 
Stop receiving emails on this subject.

Flag this post as spam/abuse.

Posted by Bill Wood on 27-Feb-2015 05:55

That is TRUE if the expression is a CHARACTER expression.  The LOGICAL function is converting a String to a Logical.  But you can also do:

MESSAGE LOGICAL(TRUE AND FALSE).   >>  no
MESSAGE LOGICAL(FALSE OR TRUE).  >> yes
MESSAGE LOGICAL(7 < 1000). >> yes

The intent of LOGICAL is to do type conversion to YES/NO based on string values.   So.

MESSAGE LOGICAL ("Femme", "Homme/Femme") >> no

Posted by Libor Laubacher on 27-Feb-2015 06:03

>> MESSAGE LOGICAL ("Femme", "Homme/Femme") >> no

MESSAGE LOGICAL("Femme","Femme/Homme"). >> yes.
 
[collapse]
From: Bill Wood [mailto:bounce-wood@community.progress.com]
Sent: Friday, February 27, 2015 12:56 PM
To: TU.OE.Development@community.progress.com
Subject: RE: [Technical Users - OE Development] AW: evaluate a string
 
Reply by Bill Wood

That is TRUE if the expression is a CHARACTER expression.  The LOGICAL function is converting a String to a Logical.  But you can also do:

MESSAGE LOGICAL(TRUE AND FALSE).   >>  no
MESSAGE LOGICAL(FALSE OR TRUE).  >> yes
MESSAGE LOGICAL(7 < 1000). >> yes

The intent of LOGICAL is to do type conversion to YES/NO based on string values.   So.

MESSAGE LOGICAL ("Femme", "Homme/Femme") >> no

Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

Posted by slacroixak on 27-Feb-2015 07:56

When it comes to evaluation on the fly, the trick of P39990 rules.  An amazing trick that can evaluate something like "2 + 3 * 4" with the right operator precedence (that is indeed 14 and not 20)

knowledgebase.progress.com/.../P39990

Posted by ecsousa on 27-Feb-2015 08:00

Thank you so much!

This thread is closed