IF statements using grouping with AND & OR

Discuss your general Proclaim related queries here.
Code Killer
Posts: 30
Joined: Thu Oct 29, 2015 10:04 am
Location: Kent
Has thanked: 27 times
Been thanked: 2 times

IF statements using grouping with AND & OR

Post by Code Killer »

Hi All,

Does any of you guys know quite how advanced the IF statement can be in Proclaim? :?:

I've been looking at something today which would be really useful if I could do something like this:

Code: Select all

IF {Field 01} = "Yes" AND ( {Field 02} = "Spoon" OR {Field 02} = "Fork" OR {Field 02} = "Spork" ) THEN
RESULT = TRUE
ELSE
RESULT = FALSE
END
I've also for a long time been thinking if there's a way to maybe do something similar to SQL arrays like:

Code: Select all

IF {Field 01} = "Yes" AND ( {Field 02} in ["Spoon","Fork","Spork"] ) THEN
RESULT = TRUE
ELSE
RESULT = FALSE
END
Thanks again

John

steve
Posts: 435
Joined: Wed Nov 30, 2011 10:20 pm
Been thanked: 100 times

Re: IF statements using grouping with AND & OR

Post by steve »

try it!

Code: Select all

va = 1 
vb = 1 
vc = 0 
IF va = 1 AND ( vb = 1 OR vc = 1 ) THEN 
    MESSAGE( "true" )  
ELSE 
    MESSAGE( "false" ) 
END 
and

Code: Select all

vlist = "spoon~fork~spork" 
# 
v1 = "yes" 
v2 = "spork" 
vin = LISTFIND( vlist , v2 , "~" ) 
IF v1 = "yes" AND vin > 0 THEN 
    MESSAGE( "True" )  
ELSE 
    MESSAGE( "False" ) 
END

Code Killer
Posts: 30
Joined: Thu Oct 29, 2015 10:04 am
Location: Kent
Has thanked: 27 times
Been thanked: 2 times

Re: IF statements using grouping with AND & OR

Post by Code Killer »

Hi Steve,

Thanks for that :D , I did try to use the first example before posting here, what's become apparent is that with either of these ways of tackling the problem is that the list I'm checking against which was created 8 years ago by a previous dev includes characters like + and & that seem to cause these functions to fail. :roll:

Is there any way to work around it? :?:

Thanks again

John

steve
Posts: 435
Joined: Wed Nov 30, 2011 10:20 pm
Been thanked: 100 times

Re: IF statements using grouping with AND & OR

Post by steve »

have you got an example of the failing code and inputs?

Code Killer
Posts: 30
Joined: Thu Oct 29, 2015 10:04 am
Location: Kent
Has thanked: 27 times
Been thanked: 2 times

Re: IF statements using grouping with AND & OR

Post by Code Killer »

Code: Select all

IF {SYS Haven Delegated.Code} = "Yes" AND ( {Claim type.Description} =  "Credit Hire" OR {Claim type.Description} = "Credit Hire + Credit Repair" OR {Claim type.Description} = "Credit Hire + Storage & Recovery" ) THEN 
     MESSAGE( "Delegated Hire & Right File Type" )  
ELSE 
        MESSAGE( "Non-Delegated Hire OR wrong file type" ) 
END
This code looks OK to me but each time I try to check it it ends up as the below when it's compiling :? , that's how it ends up:

Code: Select all

IF {SYS Haven Delegated.Code} = "Yes" AND ( {SYS Haven Delegated.Code} "Credit Hire" OR {Claim type.Description} = "Credit Hire + Credit Repair" OR {Claim type.Description} = "Credit Hire + Storage & Recovery" ) THEN 
     MESSAGE( "Delegated Hire & Right File Type" )  
ELSE 
        MESSAGE( "Non-Delegated Hire OR wrong file type" ) 
END
For the second option(below), this fails until I reduce the list to just "Credit Hire" I'm assuming the syntax in pseudocode is LISTFIND( List variable or string , search string ,"delimiter")? :geek:

Ignore the underlined text, I realised what was wrong as I was typing the syntax: :?

Code: Select all

    v-caseTypes = "Credit Hire~Credit Hire + Credit Repair~Credit Hire + Storage & Recovery" 
    v-ct = LISTFIND( v-caseTypes , {Claim type.Description} , "~" ) 
    IF {SYS Haven Delegated.Code} = "Yes" AND v-ct > 0 THEN 
        MESSAGE( "Delegated Hire & Right File Type " , NEWLINE , v-ct )  
    ELSE 
        MESSAGE( "Non-Delegated Hire OR wrong file type " , NEWLINE , v-ct ) 
    END 


Thanks again

John

steve
Posts: 435
Joined: Wed Nov 30, 2011 10:20 pm
Been thanked: 100 times

Re: IF statements using grouping with AND & OR

Post by steve »

in the 2nd block of code that is not compiling, looks like an oddity in the maths parser. I see the same issue on CHECK here.
A workaround is just assign the options to variables first, then it checks/compiles fine:

Code: Select all

va = TEXT( {SYS Haven Delegated.Code} ) 
vb = TEXT( {Claim type.Description} ) 
IF va = "Yes" AND ( vb = "Credit Hire" OR vb = "Credit Hire + Credit Repair" OR vb = "Credit Hire + Storage & Recovery" ) THEN 
    MESSAGE( "Delegated Hire & Right File Type" )  
ELSE 
    MESSAGE( "Non-Delegated Hire OR wrong file type" ) 
END 
can you test on {Claim Type.Code} instead of .Description, which is hopefully more simple and less prone to typos in tests?

re: LISTFIND syntax. Yes, look in Tech 3 manual for full details.

Code Killer
Posts: 30
Joined: Thu Oct 29, 2015 10:04 am
Location: Kent
Has thanked: 27 times
Been thanked: 2 times

Re: IF statements using grouping with AND & OR

Post by Code Killer »

Hi Steve,

I usually copy and paste the descriptions from options maintenance to make sure they're spelt correctly, mostly it's for easy reading when I need to come back to the code.

So I've tried this:

Code: Select all

IF {SYS Haven Delegated.Code} = "Yes" AND (  {Claim type.Code} =  02 OR {Claim type.Code} = 04 OR {Claim type.Code} = 03 ) THEN
and this(to see if a string would work):

Code: Select all

IF {SYS Haven Delegated.Code} = "Yes" AND (  {Claim type.Code} =  "02" OR {Claim type.Code} = "04" OR {Claim type.Code} = "03" ) THEN
In both cases the parser completely knackers them by changing the code.

Thanks again

John

steve
Posts: 435
Joined: Wed Nov 30, 2011 10:20 pm
Been thanked: 100 times

Re: IF statements using grouping with AND & OR

Post by steve »

Code Killer wrote:
Tue Nov 10, 2020 11:40 am

So I've tried this:

Code: Select all

IF {SYS Haven Delegated.Code} = "Yes" AND (  {Claim type.Code} =  02 OR {Claim type.Code} = 04 OR {Claim type.Code} = 03 ) THEN
and this(to see if a string would work):

Code: Select all

IF {SYS Haven Delegated.Code} = "Yes" AND (  {Claim type.Code} =  "02" OR {Claim type.Code} = "04" OR {Claim type.Code} = "03" ) THEN
You have to match the data type of the option column you define in DB maint,
so, if in DB maint, {claim type.Code} is an Alpha, then you certainly need the double-quotes around the values e/g/ {Claim type.Code} = "02"

This is separate from the problem with the maths parser, which doesn't seem to like nested IF clauses involving fields.

Did you try the variable substitution method workaround in my previous post?

Code Killer
Posts: 30
Joined: Thu Oct 29, 2015 10:04 am
Location: Kent
Has thanked: 27 times
Been thanked: 2 times

Re: IF statements using grouping with AND & OR

Post by Code Killer »

Hi Steve,

I've checked the {Claim type.Code} field in DB maint and thee type is alpha, so I tried using the variable solution (below) and it saved the maths but I still get the "Non-Delegated Hire OR wrong file type" message on a file which should give me the other message.

Code: Select all

va = {SYS Haven Delegated.Code} 
vb = {Claim type.Code} 
IF va = "Yes" AND ( vb = "02" OR vb = "04" OR vb = "03" ) THEN 
    MESSAGE( "Delegated Hire & Right File Type" )  
ELSE 
    MESSAGE( "Non-Delegated Hire OR wrong file type" ) 
END
I also tried with the .description as well but I got exactly the same result:

Code: Select all

va = {SYS Haven Delegated.Code} 
vb = {Claim type.Description}
IF va = "Yes" AND ( vb = "Credit Hire" OR vb = "Credit Hire + Credit Repair" OR vb = "Credit Hire + Storage & Recovery" ) THEN
    MESSAGE( "Delegated Hire & Right File Type" )  
ELSE 
    MESSAGE( "Non-Delegated Hire OR wrong file type" ) 
END
So I started Messaging out both:

Code: Select all

#===== Test 01 
va = {SYS Haven Delegated.Code} 
vb = {Claim type.Code} 
IF va = "Yes" AND ( vb = "02" OR vb = "04" OR vb = "03" ) THEN 
    MESSAGE( "Test 01" , NEWLINE , NEWLINE , "Delegated Hire & Right File Type" , NEWLINE , NEWLINE , "Claim Delegated: " , {SYS Haven Delegated.Code} , NEWLINE , NEWLINE , "Claim Type " , NEWLINE , {Claim type.Code} , " - " , {Claim type.Description} )  
ELSE 
    IF va <> "Yes" THEN 
        MESSAGE( "Test 01" , NEWLINE , NEWLINE , "Non-Delegated" , NEWLINE , NEWLINE , "Claim Delegated: " , {SYS Haven Delegated.Code} , NEWLINE , NEWLINE , "Claim Type " , NEWLINE , {Claim type.Code} , " - " , {Claim type.Description} )  
    ELSEIF vb <> "02" AND vb <> "04" AND vb <> "03" THEN 
        MESSAGE( "Wrong file type" )  
    ELSE 
        MESSAGE( "Test 01" , NEWLINE , NEWLINE , "Non-Delegated Hire OR wrong file type" , NEWLINE , NEWLINE , "Claim Delegated: " , {SYS Haven Delegated.Code} , NEWLINE , NEWLINE , "Claim Type " , NEWLINE , {Claim type.Code} , " - " , {Claim type.Description} ) 
        END 
END 
#===== Test 02 
#== 
va = {SYS Haven Delegated.Code} 
vb = {Claim type.Description} 
IF va = "Yes" AND ( vb = "Credit Hire" OR vb = "Credit Hire + Credit Repair" OR vb = "Credit Hire + Storage & Recovery" ) THEN 
    MESSAGE( "Test 02" , NEWLINE , NEWLINE , "Delegated Hire & Right File Type" , NEWLINE , NEWLINE , "Claim Delegated: " , {SYS Haven Delegated.Code} , NEWLINE , NEWLINE , "Claim Type " , NEWLINE , {Claim type.Code} , " - " , {Claim type.Description} )  
ELSE 
    IF va <> "Yes" THEN 
        MESSAGE( "Test 02" , NEWLINE , NEWLINE , "Non-Delegated" , NEWLINE , NEWLINE , "Claim Delegated: " , {SYS Haven Delegated.Code} , NEWLINE , NEWLINE , "Claim Type " , NEWLINE , {Claim type.Code} , " - " , {Claim type.Description} )  
    ELSEIF vb <> "Credit Hire" AND vb <> "Credit Hire + Credit Repair" AND vb <> "Credit Hire + Storage & Recovery" THEN 
       MESSAGE( "Wrong file type" )  
    ELSE 
       MESSAGE( "Test 02" , NEWLINE , NEWLINE , "Non-Delegated Hire OR wrong file type" , NEWLINE , NEWLINE , "Claim Delegated: " , {SYS Haven Delegated.Code} , NEWLINE , NEWLINE , "Claim Type " , NEWLINE , {Claim type.Code} , " - " , {Claim type.Description} ) 
    END 
END
Both of these present the wrong message regardless of how the fields are populated.

The other solution you gave me works perfectly and I don't need to use this now but it would be useful to know for future use.

Thanks again

John

ISimpson
Posts: 44
Joined: Mon Mar 18, 2019 5:14 pm
Location: Preston
Has thanked: 1 time
Been thanked: 6 times

Re: IF statements using grouping with AND & OR

Post by ISimpson »

One thought, have you tried a message output of both 'va' and 'vb' immediately before the IF statement to confirm the values are as you are expecting? I have been convinced several times that the values are in my variables are X and Y only to find out that they were then not what I was expecting. In other words has it been definitely ruled out that the second ELSE is actually correctly being reached?