Have an idea?

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

calling last answer in loop

For a few of my questions I am using previously entered information to display in other questions. For example in this case, I am asking if the current loop person lives in one of the places entered in A04B.  This works if A04B was only answered 1 time.  However, it could be that many places are entered in A04B.  I would want to show only the LAST place entered.  Is this possible if I have a loop of unknown iterations (between 1 and 12)?

Does [%LOOPLABEL()%] live in [%Label(A04B.1) %][%Label(A04B.2) %][%Label(A04B.3) %][%Label(A04B.4) %][%Label(A04B.5) %][%Label(A04B.6) %][%Label(A04B.7) %][%Label(A04B.8) %][%Label(A04B.9) %][%Label(A04B.10) %][%Label(A04B.11) %][%Label(A04B.12) %]</td>?</span>
asked Jan 25 by sallen Bronze (2,315 points)

1 Answer

0 votes
We just need to get the last item from the list used in the loop and call Label with that value:

[% Begin Unverified Perl
return LABEL('A04B.' . LISTVALUE('list1', LISTLENGTH('list1')));
End Unverified %]


"list1" should be the name of the loop list.
answered Jan 25 by Zachary Platinum Sawtooth Software, Inc. (205,575 points)
If the loop name is hhgrid but the list name is household2, what is wrong here? This does not call any answer...
<span class="english language">Does [%LOOPLABEL()%] live in [% Begin Unverified Perl
return LABEL('A04B.' . LISTVALUE('household2', LISTLENGTH('household2')));
End Unverified %]</td>?</span>
<span class="spanish language">Vive [%LOOPLABEL()%] en [% Begin Unverified Perl
return LABEL('A04B.' . LISTVALUE('household2', LISTLENGTH('household2')));
End Unverified %]?</span>
I tried putting the Perl script in the footer or in the html place but nothing seems to work.
What does it do?  Does it produce an error message in the admin module?
I dont see any error message.  It doesnt' really do anything...nothing shows up.
The script works on my machine, so if it doesn't work on yours your best bet would be to step through each part of it and see where things stop working as expected.  Does "[% ListLength('household2') %]" produce the output that you'd expect?  If so, does "[% ListValue('household2', ___) %] produce the output that you'd expect?  And so on.
OK.  Are you putting the code directly in the question?
I am not sure how to check these separately. would I write this to check listlength?
<span class="english language">Does [%LOOPLABEL()%] live in [% Begin Unverified Perl
return LABEL('A04B.' .LISTLENGTH('household2'));
End Unverified %]?</span>
I tried these separately but nothing shows up.  It is strange.
"[% ListLength('household2') %]" displays nothing?  Then the problem isn't with my script - why is text not showing up on your page?
Yes, it shows nothing.  if I type [%Label(A04B.1) %] it shows but not the ListLength or the ListValue one.
Maybe that's something support can look at and walk you through.  I can't say why you wouldn't be seeing anything.
OK. I will reach out to them. thanks.
...