How to keep Code short?

Hello :slight_smile:

For the following string, I’m looking for a way to keep things neat and short :

if(${Q2}= ‘C1’, ‘E1’, if(${Q2}= ‘C2’, ‘E2’, if(${Q2}= ‘C3’, ‘E3’, ’ ')))

I can’t find the correct syntax yet, in order to wrap up C1 to C244 as well as E1 to E244.

I’m asking because my current case is dealing with 244 Cs and Es – and that would make a lots of brackets )))))))….

So, I started a few threads but had no luck with this particular point on expressing arrays.

Any precision would help a lot and would be much appreciated.

If your mapping is systematic: En = Cn, you only need to replace C with E.
You may try the translate() function, see Form Operators and Functions - ODK Docs and XML Path Language (XPath).

You could also use the string functions to do such replacement,e.g. concat() and substring(), see ODK XForms Specification.

1 Like

Thanks for your reply, Wroos !

I took a deeper look at «Starting with ODK» and at the content you’re mentioning :

…but I don’t get the point yet where it should explain how to bridge C1 to C244 and E1 to E244. Could you decrypt further ?

Merci :slight_smile:

@alienationfreez, maybe you will need to share a dummy questions and choices along with your requirement for the community to understand and help you out.

Hello Kal_Lam, Wroos and everyone,

I’m searching for a way to shorten this formula :

if(${Q2}= ‘C1’, ‘E1’, if(${Q2}= ‘C2’, ‘E2’, if(${Q2}= ‘C3’, ‘E3’, ’ … … … if(${Q2}= ‘C244’, ‘E244, ’ '))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

\ (the “… … …“ stand here for the huge amount of arguments in between) \

:slight_smile:

Here is the context : How to shorten a calculation-syntax?

Here is another questioning-attempt relative to the same issue : How to shorten the Expression of an Array?

Here is an example, with 3 options (and remarks). You can test it with the online validator and adapt it to your detailed requirement.
translate_CtoE.xlsx (12.6 KB).

Would you mind to avoid, please, posting your same issue more than once to the community. It seems even three times, see here and How to shorten a calculation-syntax? and How to shorten the Expression of an Array?.

It is recommended to always test your form with the online validator, during development and before deployment, please.

Could you explain more your requirement? It seems as you might only be looking for an options to get corresponding country codes for a selection. This could be done differently (e.g. using the code as choice name or adding a look-up column or external file). There is also an international standard for country codes, see UN M49 - Wikipedia.

Thank a lot for your message, Wroos !

As I’m not very experienced in this data-management field, it is hard for me to evaluate the different approaches one can adopt to create this dial-/country-code-selector-drop-down.

I did grab a few info and started to build a first version based on what seemed to me being a way to achieve at least something visible. More experienced fellows would probably go different ways.

I came across another issue : the detailed post I started got a few answers but none of them addressing the core question which is what motivates the second and this third thread. At some point of the discussions it also became hard to get focus back on the core question and I saw a new post as the only option to get things moving forward.

I made the following description of the core issue (see screenshot) – trying my best to describe my requirements, which remain those previously mentioned in the previous threads (How to shorten a calculation syntax? / How to shorten the Expression of an Array?) :

Any help would be much appreciated.

:slight_smile:

@alienationfreez Quick question, do you specifically need to make it a string question? Because you could a select one question, and based on value you could show only one value in the next question, so it would make your life a lot easier than writing the formula

an example discussion: How to create a lookup using pull data functionality

Thanks for your feedback, @osmanburcu !
I presume sticking to the current string-question-approach will capitalize on the info and skills (even if small) I could gather so far on this topic – and even if there probably is other ways and workarounds. I’ll look into the topic you’re suggesting, but this will probably take some more time to chew.
:slight_smile:

What do you think, @wroos ?
:slight_smile:

Why? You will have to treat with various spellings for the same country, stressing analysis.