Have an idea?

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

Using modulo in Perl for list build

I frequently use RespNum() and MOD in my skip logic to achieve a split between respondents but am trying to use that same logic in PERL to create a list of question items. Possible?

I imagine it being something like this:

if(VALUE(RespNum() MOD 2)==0)
asked Jan 16, 2014 by RonH (195 points)
retagged Jan 16, 2014 by Walter Williams

1 Answer

0 votes
I believe in Perl the modulus operator is "%". So 7 % 7 = 0.

The syntax would be something like this ...

(RespNum()%2)==0

If that does not work, you can use some of the existing maths functions in Sawtooth that will do the same thing.

Example for MOD 2 ...
RESPNUM()-(int(RESPNUM()/2)*2)

Example for MOD 4 ...
RESPNUM()-(int(RESPNUM()/4)*4)

Note the "Floor" function for SSI script is written as "int" in Perl. See Sawtooth HELP.

Let me know how it goes.
answered Jan 16, 2014 by Paul Moon Platinum (66,185 points)
HI Paul,

Thank you for the quick reply. How do I call this value in the PERL? I tried a couple variations, but none successful, and I can't help but wonder if my syntax is just wrong entirely.
 
 Begin Unverified Perl  
 
 if(VALUE(RespNum()%2)==0)  
 {  
    ADD("Q20thru29all",1);   
 }  
 elsif(VALUE(RespNum()%2)!=0)  
 {  
    ADD("Q20thru29all",2);   
 }  
  
 End Unverified  
I just tested the modulus operator on a numeric question and on the respondent number and it worked fine for both.

I saved this code in a free format hidden question ...

<input name="HdMODRNUM_CODE" type="hidden" value="
[%
Begin Unverified Perl

 my $RM_code=0;
 
 $RM_code=RESPNUM()%2;

 return $RM_code;
 
End Unverified
%]
">


I believe your code does not require the "VALUE" command, just RESPNUM. Also change "RespNum()" to "RESPNUM()" for Perl programming. Make sure your question names are exactly as defined in your script as Perl is case sensitive.

Now all you need in your skip logic is HdMODRNUM_CODE==0.
...