- Posts: 3
- Thank you received: 0

# null or false equivalent in EM

1 month 2 weeks ago #199783
by Mate93

*null or false equivalent in EM*was created by

*Mate93*

What is the null or false equivalent in EM?

I am precisely trying to recreate the behavior of the implode function which won't glue the string if the argument is null/false.

In my case I have 4 variables that hold number of points in each group: SUMA, SUMB, SUMC and SUMD. There is also a variable holding max of these sums (MaxVal). I'm trying to list all the groups that were equal to the max value. This is easy if only one could be equal to max. This is not the case. I'm trying this in the text question.

If SUMB = SUMC = 10 I get the text "You've got the best score in and B and C and" instead of "(...) in B and C".

This happens because the default value for the 'result if false' in the if function is "" instead of null. I couldn't find anything that would represent the NA or NULL or false so the result would be as intended.

I am precisely trying to recreate the behavior of the implode function which won't glue the string if the argument is null/false.

In my case I have 4 variables that hold number of points in each group: SUMA, SUMB, SUMC and SUMD. There is also a variable holding max of these sums (MaxVal). I'm trying to list all the groups that were equal to the max value. This is easy if only one could be equal to max. This is not the case. I'm trying this in the text question.

`You've got the best score in: {implode(" and ", if(SUMA == MaxVal, "A"), if(SUMB == MaxVal, "B"), if(SUMC == MaxVal, "C"), if(SUMD == MaxVal, "D"))}`

If SUMB = SUMC = 10 I get the text "You've got the best score in and B and C and" instead of "(...) in B and C".

This happens because the default value for the 'result if false' in the if function is "" instead of null. I couldn't find anything that would represent the NA or NULL or false so the result would be as intended.

Please Log in to join the conversation.

1 month 2 weeks ago #199794
by Joffm

Replied by

*Joffm*on topic*null or false equivalent in EM*
First: you use the wrong syntax of the IF-statement.

It is:

IF(condition,TRUE part, FALSE part).

And the "implode"- function? It always inserts the "and" between each parameter.

So you might do this

1. an equation (str1) that only joins the MaxVals

join(if(SUMA == MaxVal, "A and ",""), if(SUMB == MaxVal, "B and ",""), if(SUMC == MaxVal, "C and ",""),if(SUMC == MaxVal,"D",""))}

2. an equation (str2) that removes the last " and "

substr(str1,0,strlen(str1)-5)

Joffm

It is:

IF(condition,TRUE part, FALSE part).

And the "implode"- function? It always inserts the "and" between each parameter.

So you might do this

1. an equation (str1) that only joins the MaxVals

join(if(SUMA == MaxVal, "A and ",""), if(SUMB == MaxVal, "B and ",""), if(SUMC == MaxVal, "C and ",""),if(SUMC == MaxVal,"D",""))}

2. an equation (str2) that removes the last " and "

substr(str1,0,strlen(str1)-5)

Joffm

*Volunteers are not paid.*

Not because they are worthless, but because they are priceless

Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

1 month 2 weeks ago #199798
by Mate93

Replied by

*Mate93*on topic*null or false equivalent in EM*
Thank you for answering

First - I use if in a short way. In manual.limesurvey.org/Expression_Manager next to the if function it states:

Your solution works only in the example I gave, not for the overal problem. The method you propose - replacing first and last "and" - is done by strictly replacing first and last 4 characters. This won't hold when only one SUM is equal MaxVal. The same for the second extreme scenario that all SUMs equal MaxVal. In the latter you have no unnecessary " and ".

Of course I could 'if' my way around adding a case for one SUM equal MaxVal, more than one SUM equal MaxVal and all SUMs equal MaxVal. Other way would be to use regex-based replacement, replacing "^ and " and also " and$" with "" but it doesn't seem possible in lime EM. This shouldn't (in my opinion) require this much workaround in code.

The other possible way would be to filter out the list and drop "" strings before passing it to implode. PHP has array filtering but I want to avoid PHP in my code. Not sure how to do it in EM. In documentation there is listifop (New in 3.16.1 ), but I can't figure out how this would work in my case as there is no use case for it.

First - I use if in a short way. In manual.limesurvey.org/Expression_Manager next to the if function it states:

- if (Updated in 3.0.2)

- Excel-style if(test,result_if_true[,result_if_false = ""])
- if(test,result_if_true[,result_if_false = ""])

Your solution works only in the example I gave, not for the overal problem. The method you propose - replacing first and last "and" - is done by strictly replacing first and last 4 characters. This won't hold when only one SUM is equal MaxVal. The same for the second extreme scenario that all SUMs equal MaxVal. In the latter you have no unnecessary " and ".

Of course I could 'if' my way around adding a case for one SUM equal MaxVal, more than one SUM equal MaxVal and all SUMs equal MaxVal. Other way would be to use regex-based replacement, replacing "^ and " and also " and$" with "" but it doesn't seem possible in lime EM. This shouldn't (in my opinion) require this much workaround in code.

The other possible way would be to filter out the list and drop "" strings before passing it to implode. PHP has array filtering but I want to avoid PHP in my code. Not sure how to do it in EM. In documentation there is listifop (New in 3.16.1 ), but I can't figure out how this would work in my case as there is no use case for it.

Please Log in to join the conversation.

1 month 2 weeks ago - 1 month 2 weeks ago #199804
by Joffm

Replied by

*Joffm*on topic*null or false equivalent in EM*
Yes, you're right.

Nowadays you may use the IF without FALSE-part.

BUT:

Here is a second solution:

Use the list-Function to get a comma-separated list.

{list(if(q1_SQ001==MaxVal,"A",""),if(q1_SQ002==MaxVal,"B",""),if(q1_SQ003==MaxVal,"C",""),if(q1_SQ004==MaxVal,"D",""))}

And if you'd like to have "and" add the str_replace-function:

{str_replace(", "," and ",list(if(q1_SQ001==MaxVal,"A",""),if(q1_SQ002==MaxVal,"B",""),if(q1_SQ003==MaxVal,"C",""),if(q1_SQ004==MaxVal,"D","")))}

Joffm

And here, sorry, a typo.

Of course

join(if(SUMA == MaxVal, "A and ",""), if(SUMB == MaxVal, "B and ",""), if(SUMC == MaxVal, "C and ",""),if(SUMC == MaxVal,"D and ",""))}

Nowadays you may use the IF without FALSE-part.

BUT:

Here is a second solution:

Use the list-Function to get a comma-separated list.

{list(if(q1_SQ001==MaxVal,"A",""),if(q1_SQ002==MaxVal,"B",""),if(q1_SQ003==MaxVal,"C",""),if(q1_SQ004==MaxVal,"D",""))}

And if you'd like to have "and" add the str_replace-function:

{str_replace(", "," and ",list(if(q1_SQ001==MaxVal,"A",""),if(q1_SQ002==MaxVal,"B",""),if(q1_SQ003==MaxVal,"C",""),if(q1_SQ004==MaxVal,"D","")))}

Joffm

And here, sorry, a typo.

Of course

join(if(SUMA == MaxVal, "A and ",""), if(SUMB == MaxVal, "B and ",""), if(SUMC == MaxVal, "C and ",""),if(SUMC == MaxVal,"D and ",""))}

*Volunteers are not paid.*

Not because they are worthless, but because they are priceless

Not because they are worthless, but because they are priceless

Last edit: 1 month 2 weeks ago by Joffm.

The following user(s) said Thank You: Mate93

Please Log in to join the conversation.