Page 1 of 1

OR logic: How do you code for multiple conditions ?

Posted: Fri Apr 07, 2006 2:41 pm
by jazzer1
How would you code something like this in TX ?

IF CODE = 1, WRITE A REC
OR IF CODE = 2 WRITE B REC
OR IF CODE = 3 WRITE C REC
OR IF.... ETC

Re: OR logic: How do you code for multiple conditions ?

Posted: Mon Apr 10, 2006 10:25 am
by jgibby
jazzer1 wrote:How would you code something like this in TX ?

IF CODE = 1, WRITE A REC
OR IF CODE = 2 WRITE B REC
OR IF CODE = 3 WRITE C REC
OR IF.... ETC
DatastageTX's If/Then does not have an ElseIF component common to many traditional languages. Also, it does not have true Case statement. What it does have is the EITHER function. Although not the same as a case statement, it can perform in a similar fashion. The premise behind it is to return the first non-null result. Here's one such use:

Code: Select all

=EITHER(
    Field1:Record:File
    ,Field2:Record:File
    ,Field3:Record:File
)
This would return the first field with a value. To use it in the way you are wanting, I would assume, make use of the PUT function. Here's how I would construct it.

Code: Select all

=PUT(
    "FILE"
    ,EITHER(
        IF(CODE = 1,"ARecFileName")
        ,IF(CODE = 2,"BRecFileName")
        ,IF(CODE = 3,"CRecFileName")
    )
    ,EITHER(
        IF(CODE = 1,ARec)
        ,IF(CODE = 2,BRec)
        ,IF(CODE = 3,CRec)
    )
)
The reason I would put the EITHER inside the FileName & FileContents arguments of the PUT function is that the PUT function does not return anything, thus if I constructed the statement like this,

Code: Select all

=EITHER(
    IF(CODE = 1,PUT("FILE","ARecFileName",ARec))
    ,IF(CODE = 2,PUT("FILE","BRecFileName",BRec))
    ,IF(CODE = 3,PUT("FILE","CRecFileName",CRec))
)
...then this might have the effect of writing all three records, since each PUT doesn't return anything, causing the EITHER to execute the next argument.

Posted: Tue Apr 11, 2006 7:25 am
by jazzer1
This is great. Thanks very much.