Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Sum of an array question (from / to)

  • apollopa
  • apollopa's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #199362 by apollopa
Sum of an array question (from / to) was created by apollopa
Hello,

I'm working on an array type question, where I need to add some values ​​according to the answers for each line.
After the sum, I need to include it in a question to be exported.


I'm starting my studies on the tool, I looked at the material, but I couldn't go any further. :(


F5_F501 [0,3,7,10,14]
F5_F502 [0,3,7,10,13]
F5_F503 [0,3,5,8,11]
F5_F504 [0,3,6,8,11]
F5_F505 [0,3,6,6,6]
F5_F506 [0,2,3,5,5]
F5_F507 [0,2,4,6,6]
F5_F508 [0,2,4,6,6]
F5_F509 [0,1,3,4,6]
F5_F510 [0,2,4,4,4]
F5_F511 [0,1,3,3,3]
F5_F512 [0,2,2,2,2]

Best regards.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #199364 by Joffm
Replied by Joffm on topic Sum of an array question (from / to)
Hi,
you did not say, how you coded the answer options.
I show it with numerical codes, like
0: 0, 1: 1, 2: 2; 3: 3, 4: 4+

What do you want to do?
You want to sum up the values in red of the selected answer
So this means:
If the answer code of the first item is 0, sum 0, if it is 1, sum 3, if it is 2, sum 7, if it is 3, sum 10, else sum 14.
Here the "else" is allowed because your question is mandatory.

Same with second, third item,...

Translated into the IF-statement:
{IF(F5_F501.NAOK==0,0,IF(F5_F501.NAOK==1,3,IF(F5_F501.NAOK==2,7,IF(F5_F501.NAOK==3,10,14))))}
Now you have the value of the first item.

now you use the sum-function
{sum(item1,item2,item3,...)}
You find these implemeted functions in the manual:
manual.limesurvey.org/ExpressionScript_-...mplemented_functions

Now we combine it.

In a question of type equation (let's call it SumF5) you enter (linefeeds here only to display better):
{sum(
IF(F5_F501.NAOK==0,0,IF(F5_F501.NAOK==1,3,IF(F5_F501.NAOK==2,7,IF(F5_F501.NAOK==3,10,14)))),
IF(F5_F502.NAOK==0,0,IF(F5_F502.NAOK==1,3,IF(F5_F502.NAOK==2,7,IF(F5_F502.NAOK==3,10,13)))),
IF(F5_F503.NAOK==0,0,IF(F5_F503.NAOK==1,3,IF(F5_F503.NAOK==2,5,IF(F5_F503.NAOK==3,8,11)))),
...
IF(F5_F512.NAOK==0,0,IF(F5_F503.NAOK==1,2,IF(F5_F503.NAOK==2,2,IF(F5_F503.NAOK==3,2,2))))
)}

Now the sum of all your items is stored in the code of this question and you can use it later by {SumF5}

Remember: If you did not change the answer codes and still use this "A1", "A2", ... you have to adapt the function (now you compare with a text) to
IF(F5_F501.NAOK=="A1",0,IF(F5_F501.NAOK=="A2",3,IF(F5_F501.NAOK=="A3",7,IF(F5_F501.NAOK=="A4",10,14))))

Joffm

By the way:
What does the "(from/to)" mean?

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: apollopa
The topic has been locked.
  • apollopa
  • apollopa's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #199384 by apollopa
Replied by apollopa on topic Sum of an array question (from / to)
Hi Joffm,

Thank you for your answer!
It's working. \o/

I was trying to do this by inserting the code in "Source" with javascript, working with "Array Object", something like the code below, but I couldn't:
Code:
  var arrPontosItens = new Array(
    [0,3,7,10,14],
    [0,3,7,10,13],
    [0,3,5,8,11],
    [0,3,6,8,11],
    [0,3,6,6,6],
    [0,2,3,5,5],
    [0,2,4,6,6],
    [0,2,4,6,6],
    [0,1,3,4,6],
    [0,2,4,4,4],
    [0,1,3,3,3],
    [0,2,2,2,2]
  );
 
  var nPontos = 0;
 
  for (var i=0;i < SubQuestions.length;i++){
    var code = AnswerSubQuestion(i);
    nPontos += arrPontosItens[i][code];
  }
 
 
  SetAnswer(SumF5,nPontos);


About "from / to", maybe I expressed it badly, but that was exactly it:
0: 0, 1: 1, 2: 2; 3: 3, 4: 4+

Thanks,
Bruno
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #199393 by DenisChenu
Replied by DenisChenu on topic Sum of an array question (from / to)
Not a devlopemnt, because it can be done with expression
I move it after answered

With a equation question type , i prefer to use (more easy to do with spreadsheet)
Code:
{sum(
 
intval(F5_F501.NAOK==0) *0,
intval(F5_F502.NAOK==1) *3,
intval(F5_F502.NAOK==2) *7,0)}

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: apollopa
The topic has been locked.
  • apollopa
  • apollopa's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #199402 by apollopa
Replied by apollopa on topic Sum of an array question (from / to)
Hi Denis,

Thanks for the message!


I'm a junior developer, but I've worked with other research tools.

Regarding the use of a question like "equation" or inserting the code in "source", do you believe that the best option would be "equation"?

As I am not familiar with the tool, I am concerned with finding the same situation that requires me to keep repeating the code (with many "if").

Can I use a "for" in the "equation"?

Thanks,
Bruno
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #199409 by DenisChenu
Replied by DenisChenu on topic Sum of an array question (from / to)
manual.limesurvey.org/Question_type_-_Equation : best is to use dedicated part; you can add your line feed etc …

Else : manual.limesurvey.org/Expression_Manager

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: apollopa
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago - 3 years 11 months ago #199494 by holch
Replied by holch on topic Sum of an array question (from / to)
This is obviously the CCEB (Criterio Brazil) and it is possible to create this with equations, but is a little bit of work. I have done it before, need to check if I can find it.

I would go with one equation per subquestion first in order to not mess anything up, because if you do it all in one equation, it can become quite messy.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

Last edit: 3 years 11 months ago by holch.
The following user(s) said Thank You: apollopa
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #199501 by DenisChenu
Replied by DenisChenu on topic Sum of an array question (from / to)
It's the reason why i use sum.
If you have a lot of subquestion : it can became a real big machine ?

No ?

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: apollopa
The topic has been locked.
  • apollopa
  • apollopa's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #199555 by apollopa
Replied by apollopa on topic Sum of an array question (from / to)
Hi Holch,

Exactly!

I said I needed to make a sum, because with this information I would do the rest of the code. But yes, it is the CCEB!
This classification is made in most studies here in Brazil.

If you find this code, it would help me a lot! :woohoo:

Best regards,
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #199556 by DenisChenu
Replied by DenisChenu on topic Sum of an array question (from / to)
I think you have the answer no ?

Some code sample, you just have to adapt …

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: apollopa
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #199560 by holch
Replied by holch on topic Sum of an array question (from / to)
Code:
If you find this code, it would help me a lot!

Bruno, I think we have met already here in São Paulo in your office in Pinheiros at your previous company. ;-)

I have found one version that one of our interns has programmed a while ago as an experience (might not be the latest version of the CCEB, not sure on which vesion he has based this on). I can't remember if we tested it thoroughly and if everything works 100% correct, but I did a quick check and it seems to work.

Send me a private message and I'll send you the LSS file. We had separated each score of each question/subquestion into a separate Equation in this case, because this way it was easier for him to understand how this works and also to find errors. So there are quite a few equations in this example.

I might have another version of the CCEB with everything in one equation, but I am not 100% and haven't found it yet.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The following user(s) said Thank You: apollopa
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #199565 by Joffm
Replied by Joffm on topic Sum of an array question (from / to)
As Denis,
what do you need more?
You have two options.
The first, which you said "It's working".

Here holch recommended to split the equation into parts.
One equation for each row, and at the end an equations that sums up the results.

Then the version with "intval".
As Denis said: This is easy to construct in a spreadsheet.
Like the concatination of the values

Then you copy and paste it into your question.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: DenisChenu, apollopa
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose