Have an idea?

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

Display separate graphic/text based on loop iteration

I have created a loop (securityLoop) that is based on a list (securityTypesLoopList).

This works well as I'm using [% LoopLabel(securityLoop) %] just fine.

The issue is, on the first question for the loop I'm not only displaying the text for LoopLabel(securityLoop), but I need to display a second (graphic) and third (text) dynamic piece based on the loop iteration.

I'll poste the short code below, but here is a rundown.
     LoopIteration == 1
             LoopLabel(securityLoop) shows "Local Security"
             The graphic should display a picture of their uniform
             The second dependent text is a description of the responsibilities of local security

     LoopIteration == 2
             LoopLabel(securityLoop) shows "Sherrif's Department"
             The graphic should display a picture of the Sherriff's uniform
             The second dependent text is a description of the responsibilities of the Sherrif's department

Here is a test piece of code I've been tinkering with.

[% Begin Unverified Perl
if (LoopIteration() == 1)    {
    return "Local security";
    return "<img src='[%GraphicsPath()%]Security_Uniform.PNG' alt='' border='0'>";
    elsif    (LoopIteration() == 1)    {
        return "The sherrif";
        return "<img src='[%GraphicsPath()%]Sherrif_Uniform.PNG' alt='' border='0'>";
End Unverified %]
asked Jun 10, 2021 by Nwiggin Bronze (1,785 points)
As an FYI for the future, there are two problems in your code.

First, we cannot call Sawtooth Script using the normal "[% %]" syntax within unverified Perl.  Instead, we need to call it like we would any other Perl subroutine:

return "<img src='" . GRAPHICSPATH() . "Security_Uniform.PNG' alt='' border='0'>";

Note that Sawtooth Script function names are written in all caps when doing this.

Second, a block of unverified Perl (or a function in any programming language) will cease running once it has reached any "return" statement.  To return two different texts, they will need to be joined by some means as part of the function, then we can return the combined text.  For example:

my $text = "Local security";
$text .= "<img src='" . GRAPHICSPATH() . "Security_Uniform.PNG' alt='' border='0'>";
return $text;

1 Answer

0 votes
Could you just create another couple lists with the graphics (PictureList) and descriptive text (DescriptionList) that correspond 1:1 with your SecurityTypesLoopList labels? Then you'd use LoopValue() within the ListLabel function to recall which loop you're on and tell the ListLabel function which picture/description to recall. In the question text, you'd show the LoopLabel(securityLoop) on the first row, ListLabel(PictureList,LoopValue()) on the second, and ListLabel(DescriptionList,LoopValue() on the third. And no unverified perl required!
answered Jun 10, 2021 by Aaron Hill Gold Sawtooth Software, Inc. (12,245 points)
Oh yeah...didn't think of that.  worked perfectly.  Thanks!