Desktop User Guides > Professional > Interview scripting > Writing interview scripts > Displaying information on a page > Text substitution > Displaying loop control values in repeated questions
 
Displaying loop control values in repeated questions
Loops are a flexible and efficient way of defining repeated questions (see Repeated questions). The number of repetitions can be specified using a numeric range or a list of categorical texts, both of which are called the loop control list. If you are displaying each repetition of the questions on a separate page, you might want to display the current value of the loop control item in the question texts. For example, if you are asking the same set of questions for each person in the household, you might want to refer to the people as Person 1, Person 2, and so on. Similarly, if you are asking questions about a list of brands, you might want to include the name of the current brand in the question text.
Displaying the current value of the loop control item in a question or banner text
Type this command in the question text where you want the substituted text to appear.
{@LoopName}
Parameter
LoopName
The name of the loop.
Example
If the loop is defined in the metadata section as:
RateLoop loop {
Knowledge "Subject knowledge",
Presentation "Presentation skills",
Interest "Ability to hold my interest",
Friendly "Friendliness/approachability",
Problems "Ability to deal with problems"
} fields "How would you rate the trainer for ...<p/>" (
Rating "{@RateLoop}" categorical [1..1]
{
Excellent, VGood "Very Good", Good,
Poor, VPoor "Very Poor"
};
) expand;
each question page starts with the general question text defined after fields, then a blank line, and then the item to be rated:
This graphic is described in the surrounding text.
If the substitution does not work and you see the marker rather than the text it represents, check that you typed in @ in front of the marker name and that the marker name is spelled correctly.
Nested loops
When a loop is nested (that is, one loop inside another) and you want to substitute the value of the inner loop control list in the question text, you need to use the full name of the inner loop that shows its relationship to the outer loop.
Syntax
{@OuterLoopName.InnerLoopName}
Parameters
OuterLoopName
The name of the outer (parent) loop.
InnerLoopName
The name of the inner (child) loop.
Example
This example shows what a nested loop might look like during an interview. Each question is displayed on a separate page and shows the values of the inner loop (attribute to be rated) and the outer loop (course section) in its text.
This graphic is described in the surrounding text.
The metadata section that defines the loop and the text substitutions is:
CoursePart loop
{
Architecture "Overview of system architecture",
Installation "Server installation",
Performance "Performance monitoring",
Troubleshooting, Security,
Access "Implementing access plans"
} fields (
RateFor loop
{
Relevance "Relevant to my job",
Content "Information provided",
Materials "Training materials",
Exercises "Exercises"
} fields (
Rating "Please rate the <I>{@CoursePart}</I> section
of the course for <I>{@CoursePart.RateFor}</I>."
categorical [1..1]
{
Excellent, VGood "Very Good", Good, Poor,
VPoor "Very Poor"
};
) expand;
) expand;
CoursePart is the outer loop and defines the part of the course that are to be rated. Its current value is substituted in the Rating question text using {@CoursePart}. RateFor is the inner loop and defines the aspects to be rated for each course part. Its current value is substituted in the Rating question using {@CoursePart.RateFor}. Notice the use of standard HTML tags to display these texts in italics.
If you want to see this example working, copy it into your own script and add the following code to the routing section (see “Each Question on a Separate Page” in Asking repeated questions for an explanation):
Dim training_section, attribute, question
For Each training_section In CoursePart
For Each attribute In training_section
For Each question in attribute
question.Ask()
Next
Next
Next
Abbreviations and shortcuts with @
The interviewing program numbers the levels in nested loops relative to one another, so that the current loop is level 1, the next level out (the parent) is level 2, and the next level beyond that (the grandparent) is level 3, and so on. Rather that typing the hierarchical loop names when you want to substitute loop values in question texts, you can use these level numbers instead.
{@} or{@1} is an abbreviated way of substituting the value of the current loop in question texts. In the example you could use either notation instead of {@CoursePart.RateFor}. You could also use {@2} instead of {@CoursePart} to substitute the value of the parent loop.
The advantage of using the fuller syntax is that it is immediately clear what is being substituted, so it helps in debugging problems in the script. The disadvantage is that the code is not immediately portable if you want to copy and paste just the inner section of a nested loop. You always need to edit the substitution marker before the code will work.
See also
Text substitution