Have an idea?

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

Conditional Operands

Does the software support operands that are like "contains" or "starts with"?

For example,

[%Begin Unverified Perl
if(QUESTIONNAME() <begins with> "q")
blah, blah, blah
End Unverified%]



... or recognize question type?
[%Begin Unverified Perl
if(QUESTIONTYPE() == ACAPairs)
blah, blah, blah
End Unverified%]



I know "questiontype" is not a current supported script, i am just curious about the concept.



Thanks.
asked Feb 14 by WTW_cal Bronze (1,455 points)

1 Answer

0 votes
Lighthouse Studio's Sawtooth Script function TextExists can be used to determine if a string contains a substring.

There is no built-in Sawtooth Script to determine if a string starts with a substring.  You'll likely need to use regular expressions for that.  Here is a Perl snippet that will run if the current question's name begins with "q":

if (QUESTIONNAME() =~ m/^q/) {
    ...
}


I can offer more regular expressions if you know what you want to check for in the question name.

I don't believe there's any way to determine the question type in unverified Perl without some major trickery, but that sounds like a cool idea.
answered Feb 14 by Zachary Platinum Sawtooth Software, Inc. (95,875 points)
Thanks Zachary. Can that be expanded, so instead of simply 'q' it's several characters, like 'MATRIX_P'? This is somewhat a workaround to the question type idea. "MATRIX" is the name of my ACA design and "_P" refers to the pairs questions.
I see.  That's a classic gotcha: the code runs for each page of the exercise, but you only want it for a subset of those pages.  If you replace my "q" with "MATRIX_Pair", you should be good to go.  Alternatively, using TextExists to check for "_Pair" ought to work if you want to avoid regular expressions.
Ideally I want this code to sit in my global header, so it universally covers everything. I am unsuccessful with that, but I am successful with the script at the question level itself, which somewhat defeats the original ask.
Yeah, I'm afraid QuestionName does not work in the global header as survey pages are not restricted to one question per page.  Perhaps there are other acceptable alternatives.

I don't know what you are interested in doing on Pair questions, but it may be possible to do with JavaScript instead of Perl.  JavaScript could determine if the page has a Pair question on it and do certain things only on those pages.

If you need to stay in unverified Perl rather than JavaScript, perhaps PageNumber would be helpful.  If you know the page number of the first and last Pair questions, you could run your Perl only on pages between those numbers.
...