Have an idea?

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

what is the error in the below syntax, this creates sawtooth error #132

Begin Unverified Perl

 if (GETVALUE("A4") < 15) {
    SETVALUE("A4Grp", 1);
   
}
else if (GETVALUE("A4")>=15 && GETVALUE("A4")< 19) {
    SETVALUE("A4Grp", 2);
}

End Unverified


I have written this code in pre-skip logic of A4Grp, where A4Grp is select type question contains around 8 options.

If I use only the first if condition it didnt create any error but it would set any values in A4Grp, means that SetValue function doesnt work properly.
related to an answer for: How to add multiple if conditions?
asked Jun 17 by raaamuz (360 points)

1 Answer

0 votes
With Unverified Perl you need to use elsif for your second statement, not 'else if'.  It's just one word with no spaces and no 'e'.  That should solve your problem. See below.

Begin Unverified Perl
 
 if (GETVALUE("A4") < 15) {
    SETVALUE("A4Grp", 1);
    
}
elsif (GETVALUE("A4")>=15 && GETVALUE("A4")< 19) {
    SETVALUE("A4Grp", 2);
}
 
End Unverified
answered Jun 17 by Jay Rutherford Gold (32,615 points)
How about this syntax? this is also creating the same error


Begin Unverified Perl
 
 my $a=0;
 
 if (GETVALUE("A4")<15)
  {
   SETVALUE("A4Grp",1);
   return $a=1;
  }
 elsif (GETVALUE("A4")>=15 && GETVALUE("A4")<19)
  {
   SETVALUE("A4Grp",2);
   return $a=1;
  }
 elsif (GETVALUE("A4")>=20 && GETVALUE("A4")<24)
  {
   SETVALUE("A4Grp",3);
   return $a=1;
  }
  else
  {
      SETVALUE("A4Grp",4);
       return $a=1;
  }
}
   
End Unverified
Sawtooth Error # 132
Script error.

Please try to refresh your browser or try to backup and submit again. If the error continues please contact the survey administrator.
So you are setting the value for A4Grp correctly.  The issue is with the return $a1=1 statement.  What is it you are trying to achieve there?  You can make that value =1 to one or return that variable to the screen but I don't believe you can use return to change the value of '$a'.  I'm not clear on what you want to happen there.
I am newbie to perl as well as sawtooth, the syntax is actually not mine. someone in the forum has suggested this. even I tried without return statement but it hadn't mark anything in A4Grp
Okay, so just so I'm clear.  You have created a Pass-In Variable in the program and named it 'A4Grp'?  You want to set that value based on the response to A4.  I believe one issue is how you are calling Unverified Perl.  If it is in a constructed list, how you have it would be correct.  However, if you are using it one a page you need to call it with the SSI Script tags [% %].  So, make sure the below script is on a page after A4 that all respondents will get.  I changed your second statement to less than 20 instead of 19, otherwise an answer of 19 would not have fit anywhere.

[% Begin Unverified Perl
 
  if (GETVALUE("A4")<15)
  {   SETVALUE("A4Grp",1)  }
 elsif (GETVALUE("A4")>=15 && GETVALUE("A4")<20)
  {   SETVALUE("A4Grp",2)  }
 elsif (GETVALUE("A4")>=20 && GETVALUE("A4")<24)
  {   SETVALUE("A4Grp",3)  }
  else  {  SETVALUE("A4Grp",4)  }
 
End Unverified %]
I have included this syntax in the footer window of A4Grp but it didn't mark anything when I punch 16 in A4. can you tell what did I miss here
At this point I would need to see a sample of your ssi file to determine what the issue is.  Feel free to email me at jruth@isgmn.com and I'll be happy to take a look.
I have a few thoughts about these two questions, if I may.

Error 132 shows up when an unexpected error occurs in unverified Perl.  If you go to your admin module, you can find the full details of the error, which often provides some insight about what is wrong with a particular script.

If you want a response of A4Grp pre-selected, you may need to use some JavaScript instead of SetValue.  Assuming A4Grp is a select question, perhaps something like this would be helpful:

<script>
$(document).ready(function(){
    var a4 = [% A4 %];
    if (a4 < 15) {
        SSI_SetSelect('A4Grp_1', true);
    }
    else if (a4 < 20) {
        SSI_SetSelect('A4Grp_2', true);
    }
    else if (a4 < 24) {
        SSI_SetSelect('A4Grp_3', true);
    }
    else {
        SSI_SetSelect('A4Grp_4', true);
    }
})
</script>
Thanks for the additional detail Zach.  I see that could be the problem, if that is what he has done.  If the question A4Grp was set up instead of a Pass-In Variable, I would think it would be a single select.  If so, then they may want to pre-skip A4Grp and put the perl code I provided in a question after A4Grp that all would see.  I commonly do this when I ask an open numeric question but they also want them grouped categorically.  Either set up a question that is skipped or set up a Pass-In variable instead.  Both work well.
This works well only when we write this syntax on A4Grp, but A4Grp is a hidden question so I was trying to logic when it exit from question A4, do you have any other alternate solution?
If A4Grp is a hidden question and a single select then the script I provided will work as long as you place it on another question after A4.  Then it will store the value for A4Grp as you want.
Currently I placed A4Grp next to A4, do you want me to move A4Grp one question ahead? since it is a hidden question I am fine with keeping A4Grp anywhere but I just want to clearly understand your context, please confirm
The key is to put the script on a page AFTER A4 as it is calling upon the submitted responses to A4.  As long as you do that, it will set the responses for A4Grp using that script.
...