The get_answer
method retrieves a question's response for use in filling fields or displaying values. This is often called piping a response.
1. Please rate the pizza on a scale from 1 to 5, 1 being the "worst" and 5 being the "best."
type: radio
series: 1..5
hscale: y
headings: worst, best
1A. Your response was [* get_answer('Q1') *]. Why?
type: text
Details
- The
get_answer
method retrieves data from the same survey, allowing access to responses from the same respondent or a different respondent ID. - At a minimum, it's required to specify the question ID, written as a string, enclosed in single quotation marks (e.g.,
get_answer('Q1')
). - By default,
get_answer
returns the raw response (option ID for closed-ended questions, text for open-ended questions). - You can use specific parameters to customize the retrieved content.
- The most common
get_answer
parameters have been built as aliases for programming efficiency. - When working with tables, use
get_answer
on individual fields (e.g.,Q8R1
for a table row). - If
get_answer
is used in question or option text (outside a Perl environment), the piping requires square brackets and asterisks, e.g.,[* *]
, as shown in the example above. -
get_answer
can be used oncvalue
to record a value for a variable.
Tip! For more information on using get_answer
to access data from other surveys present on the same server (for example, a recontact survey), see allowreadsfrom
.
Parameters
You can customize the data returned by applying the parameters below.
Parameter | Description | Example code |
final |
Adds a word (e.g., "and") between the last two options in multi-select questions. | get_answer('Q1', separator=> ", ", final=> " and ") |
limit |
Limits the number of responses retrieved for multi-select questions (e.g., |
get_answer('Q1', limit => 1) |
loop |
References a specific iteration for looped questions. The |
|
oxford |
The
|
get_phrase('Q1', oxford => 0) |
separator |
Joins multiple values with the specified character, defaulting to a comma. |
get_answer('Q1', separator => ", ") |
text |
Retrieves option text instead of ID for closed-end responses (e.g., |
get_answer('Q1', text => 1) |
Aliases
These are the most common aliases.
Alias | Description | Example code | Same as |
get_answer_text |
Retrieves option text for single-select and multi-select responses. |
|
|
get_first_answer |
Same as get_answer , but retrieves only the first response. |
|
|
get_first_answer_text |
Same as |
|
|
get_phrase |
For multi-select questions, combines responses with appropriate separators, using an Oxford comma by default. |
|
|
Note: When translating a survey, the final "and" in get_phrase
will not automatically translate. To address this, create a set text
widget for "and" and use get_answer_text
with the set text
widget, along with any other parameters needed. For example, get_answer_text('Q1', separator=> ", ", final => "__AND__ ", oxford => 1)
.
Additional examples
Piping in text, basic usage
This example shows how both get_answer
and the alias get_answer_text
pipe the text of selected options from the referenced question.
Adding "and" with 'get_phrase'
Using get_phrase
creates outputs like "Option 1 and Option 2" or "Option 1, Option 2, and Option 3". It pipes in all selected answers from a previous checkbox question, adding separators and a final joining word (e.g., "and") as needed.
Now try the example below by selecting two or three responses. Compare this result with the previous example
Using 'get_answer' with tables
Each table row corresponds to a unique data field. To pull the answer for a specific row, use get_answer
to reference that field directly rather than the entire table. For example, get_answer_text
can reference Q8R1 to pipe the answer from the "Exercising" row.
Comments
0 comments
Please sign in to leave a comment.