Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Perl usage in skip logic

Hi,
could you help me to understand how to use perl in skip logic?
I need to count respondents with certain criteria in 40 questions, and if there are less then 10 out of 40 than go to certain question (skip to).

Thanks,
Alex
asked May 16, 2016 by Alex (300 points)

1 Answer

+2 votes
If you are talking about answering 10+ items in a question, you can use the NumChecked function in a skip.

e.g. if Q1 is answered greater than 10 items then skip ...

Use NumChecked(Q1)>10 as your skip logic.

If you need to apply 40 conditions across 40 different questions / criteria sets, you can setup a parent list with a description for each criteria.

Parent list (call it CriteriaList)
1-Q1 criteria (example - Q1=1)
2-Q2 criteria (example - Q2_5=1)
3-Q3 criteria (example - Q3>2)
4-Q10 criteria (example - Q10_r1=3)
and so on ...

Now create a constructed list using the parent list above. Call it CriteriaConList.
Begin Unverified Perl 
  
 if (VALUE("Q1")==1)
  {  
   ADD("CriteriaList",1);
  }

 if (VALUE("Q2_5")==1)
  {  
   ADD("CriteriaList",2);
  }

 if (VALUE("Q3")>2)
  {  
   ADD("CriteriaList",3);
  }

 if (VALUE("Q10_r1")==3)
  {  
   ADD("CriteriaList",4);
  }

End Unverified

As each criteria set is true, the respective item is added to the constructed list - CriteriaConList.

Now in your skip logic, you can use ListLength(CriteriaConList)>10, then skip ...
answered May 16, 2016 by Paul Moon Platinum (65,130 points)
edited May 16, 2016 by Paul Moon
If you don't need the database to store which criteria was met, you can alternatively put the Perl directly into the skip logic.  A skip with this logic will do the same thing without the use of lists:

Begin Unverified Perl
my $var = 0;
if (VALUE('Q1') == 1) {
    $var++;
}
if (VALUE('Q2_5') == 1) {
    $var++;
}
if (VALUE('Q3') > 2) {
    $var++;
}
if (VALUE('Q10_r1') == 3) {
    $var++;
}
# More criteria...
return $var > 10;
End Unverified
The two positives with the list building is it does assist with the testing and analysis. If these are not relevant to you, Zachary's method is spot on (as usual).
You've got me convinced.  When Perl gets even a little complex, it can become a nightmare to debug.  List building can surely provide a valuable clue for finding what is wrong.
...