Have an idea?

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

Add-if with unverified perl

Hi there.

I am trying to add the list member of a previous page's Grid question if they answer a certain way. The bulleted list could be 1-7 options, depending on how many are selected as a certain value. This is what I came up with but it only works for one bullet (whatever is first selected as a 3 previously).

I am sure there's a way to simplify this down and ensure that it's additive (i.e., if 4 items are previously valued at 3, then those same four are bullet listed on the successive page).

Any help would be appreciated. Thanks.

How can we address each of the following?
<ul>
[% Begin Unverified Perl
if(GETVALUE("Questions_r1")==3){return "<li>" . LISTLABEL("List",1) . "</li>";}
if(GETVALUE("Questions_r2")==3){return "<li>" . LISTLABEL("List",2) . "</li>";}
if(GETVALUE("Questions_r3")==3){return "<li>" . LISTLABEL("List",3) . "</li>";}
if(GETVALUE("Questions_r4")==3){return "<li>" . LISTLABEL("List",4) . "</li>";}
if(GETVALUE("Questions_r5")==3){return "<li>" . LISTLABEL("List",5) . "</li>";}
if(GETVALUE("Questions_r6")==3){return "<li>" . LISTLABEL("List",6) . "</li>";}
if(GETVALUE("Questions_r7")==3){return "<li>" . LISTLABEL("List",7) . "</li>";}
End Unverified %]
</ul>
asked Nov 18 by Colin Linerode Bronze (1,695 points)
I believe Jay is correct about using AIE and FormatListLabels.  Still, we can talk about the code for the sake of learning.  In programming, "return" terminates the current function being ran (in this case, the unverified Perl block).  If you want to "return multiple things" like here, you need to create some variable(s) in the code, save the values you want returned to it, then return the whole thing.  Look at this example:

[% Begin Unverified Perl
my $output = '';
if (GETVALUE('Questions_r1') == 3) {
    $output .= '<li>' . LISTLABEL('List', 1) . '</li>';
}
if (GETVALUE('Questions_r2') == 3) {
    $output .= '<li>' . LISTLABEL('List', 2) . '</li>';
}
if (GETVALUE('Questions_r3') == 3) {
    $output .= '<li>' . LISTLABEL('List', 3) . '</li>';
}
...
return $output;
End Unverified %]


Like you said, this can be simplified.  Whenever you find yourself doing work like this - copy-and-pasting code with just changed values - we can consider using a loop instead.  Try this:

[% Begin Unverified Perl
my $output = '';
for (my $i = 1; $i <= 7; $i++) {
    if (GETVALUE('Questions_r' . $i) == 3) {
        $output .= '<li>' . LISTLABEL('List', $i) . '</li>';
    }
}
return $output;
End Unverified %]

1 Answer

0 votes
Are they already formatted the way you want in the grid question?  If so you can just create a constructed list with AIE (gridquestion, 3) and it will create a list of all the items rated 3 in your grid question.  Or is your issue about showing the list on the screen as bulleted items?
answered Nov 18 by Jay Rutherford Platinum (52,065 points)
The latter -- showing which grid items were rated as a 3 in a bulleted list. The bulleted list is just static text. Thanks.
Okay, two different Sawtooth scripts to help with showing lists.  One is FormatListLabels and the other is DisplayListLabels.  Take a look at that in the help menu.  You would want to create a constructed list as I indicated before and then show it like this (I'll call the list SelectedList:
FormatListLabels (SelectedList,"<li>","/li>")
...