Variables and arrays let you remember things about each respondent as they move through your survey—a running quiz score, a label like "Promoter", a list of brands they mentioned—and use them later to pipe text, show or hide questions, or set the order of answers. This article walks through how they work, with examples. For a compact list of every operator and command, see the aytm Logic Reference Guide.
In This Article
- Variables
- When Variables Update
- Naming Variables
- Example: Quiz Score
- Arrays
- Example: Spotting a Country in Open-Ended Answers
- Example: Respondent Labels
- Shuffling, Sorting and Cleaning Up Arrays
- Ordering Answers with an Array
- Reserved Variables
Variables
A variable holds one value—a number or text—for each respondent. You don't need to declare it first: the first time logic sets it, the variable exists. A variable that hasn't been set yet is empty, so piping it shows nothing.
| Logic | What it does |
|---|---|
[QuizScore = 0] |
Sets the variable to 0. The right side can be a calculation, e.g., [Total = Q1A1cv + Q1A2cv]. |
[QuizScore + 1] |
Adds 1 to the current value. If the variable hasn't been set yet, it starts from 0. |
[QuizScore - 5] |
Subtracts 5 from the current value. * and / work the same way. |
[QuizScore] |
Pipes the current value into the text the respondent sees. |
[NPS = "Promoter"] |
Sets the variable to the text Promoter. Text goes in quotes. |
[NPS + " and Influencer"] |
Adds text to the end, giving Promoter and Influencer. No space is added for you, so put it inside the quotes. |
[show if NPS = "promoter"] |
Uses the variable in a condition. Text comparisons with = ignore upper and lower case. |
When Variables Update
- On submit. Logic that sets a variable runs when the respondent submits the question it's typed in. The new value is available from the next question on, not inside the same question.
- On an answer option. Logic typed on an answer option runs only if the respondent selects that answer.
[QuizScore + 1]on the correct answer adds a point only for respondents who chose it. - With a condition. Add
ifto run it only sometimes:[QuizScore + 1 if Q3A2].
Naming Variables
You choose the name. "QuizScore" and "NPS" above are just examples.
- Use letters (A–Z, a–z), digits (0–9), and underscores, starting with a letter.
- Upper and lower case don't matter:
QuizScoreandquizscoreare the same variable. - Don't start a name with Q or PQ followed by a digit (
Q1score), or use A followed only by digits (A1). aytm Logic reads those as references to questions and answers. - Don't use the name of a reserved variable (such as
age,gender,device,uuid) or a logic word (such asshow,hide,if,and,or,not,in,like,max,group,sort,exit).
Example: Quiz Score
Your survey includes a three-question quiz, and you want to show each respondent their score.
- In the question before the quiz, type
[QuizScore = 0]. This makes a respondent with no correct answers see 0 instead of a blank. - On the correct answer of each quiz question, type
[QuizScore + 1]. - After the quiz, show the result: You answered [QuizScore] out of 3 questions correctly.
- Optionally, show a follow-up only to high scorers:
[show if QuizScore >= 2].
💡 Tip: While you build the survey, pipe
[QuizScore]into a question you'll see in preview to check that the value changes as expected. Remove it before launch if respondents shouldn't see it.
Arrays
An array is a list of values—numbers, text, or both—stored in one variable. Use an array when you want to:
- Pick out one item by its position
- Show the whole list to the respondent, separated by commas
- Check whether a value is in the list
- Check whether a respondent's text contains any item in the list (for example, any spelling of a brand name)
- Set the order of answer options
| Logic | What it does |
|---|---|
[ar << 1] |
Adds 1 to the end of ar, creating the array if it doesn't exist yet. |
[ar << 1,2,3,4] |
Adds several items at once. |
[ar_1 << "first", "second", "third"] |
Adds text items. Numbers and text can be mixed: [v4 << '0', 1, 2, 3]. |
[ar3|3] |
Pipes the third item. Positions start at 1. |
[ar3] |
Pipes the whole array, separated by commas. |
[ar4|3 + 1] |
Adds 1 to the third item and saves it, like [QuizScore + 1] does for a variable. -, *, and / work too. |
[show if ar4|5 = 7] |
Shows the field if the fifth item equals 7. |
[show if ar3 = ar4] |
Shows the field if both arrays hold the same items. Order doesn't matter. |
[show if "first" in ar_1] |
Shows the field if ar_1 contains the item first. |
[show if ar_1 like "st"] |
Shows the field if any item in ar_1 contains the text st. |
[show if Q5rc like brands] |
Shows the field if the open-ended answer to Q5 contains any item in brands. |
⚠️ Note:
likeis case-sensitive and matches parts of words. "USA" doesn't match "usa", and "US" also matches "USB" or "AUSTRALIA".inand=compare whole items and ignore case.
An array holds up to 500 items; anything added beyond that is dropped. Positions run from |1 to |500. Adding items with << to a variable that holds a single value replaces that value with a new array.
Example: Spotting a Country in Open-Ended Answers
Q5 asks, in an open-ended question, where the respondent's most exciting vacation took place. You want to ask follow-up questions to anyone who mentions the United States, however they spell it.
- In an early question, list the spellings you expect:
[usvariants << "USA", "U.S.A.", "U.S.", "United States", "united states", "America", "america"]. Becauselikeis case-sensitive, add lower-case versions of the spellings respondents are likely to type. - On each follow-up question, type
[show if Q5rc like usvariants].
Example: Respondent Labels
During the survey you assign labels such as "early bird" or "influencer". Instead of a separate variable for each label, keep them all in one array.
- On the answer that qualifies a respondent, add the label:
[labels << "influencer"]. - On Q10, type
[show if "early bird" in labels]. - On Q11, type
[show if "influencer" in labels].
Shuffling, Sorting and Cleaning Up Arrays
| Logic | What it does |
|---|---|
[scores = shuffle scores] |
Puts the items in random order. |
[arr2 = shuffle arr1] |
Shuffles arr1 and saves the result in arr2. arr1 is unchanged. |
[scores = shuffle scores max 2] |
Shuffles and keeps only 2 items, e.g., to pick 2 random items out of 5. |
[scores_reversed = reverse scores] |
Reverses the order: the last item becomes the first. |
[scores_unique = unique scores] |
Removes duplicates, so each item appears once. |
[scores_sorted = sort scores] |
Sorts from lowest to highest. |
[scores_desc = reverse sort scores] |
Sorts from highest to lowest. |
How sort orders items:
- An array of numbers sorts by value: 1 < 10 < 11 < 100.
- If the array holds any text, every item is compared as text, character by character. Digits come before letters, and capitals before lower case: "1" < "10" < "100" < "11" < "Apples" < "apples".
Ordering Answers with an Array
Use [sort … by …] to show a question's answers, sub-questions, or entities in the order an array lists them. The array holds element numbers in the order you want them shown.
| Logic | What the respondent sees |
|---|---|
[CustomOrder << 4,3,2,1][sort Q2A by CustomOrder] |
Q2's answers in reverse: A4, A3, A2, A1. |
[CustomOrder << 2,3,1][sort Q2A by CustomOrder] |
A2 first, then A3, then A1. |
[sort Q2A by CustomOrder if gender = "m"] |
The custom order for male respondents only. Everyone else sees the editor order. |
[sort Q2SQ by CustomOrder] |
Orders Q2's sub-questions instead of its answers. Use Q2E for entities. |
[CustomOrder << 1,2,3,4][CustomOrder = shuffle CustomOrder][sort Q2A by CustomOrder][sort Q3A by CustomOrder] |
Q2 and Q3 answers in the same random order. |
- Unlisted elements keep their editor order and appear after the listed ones.
- Repeated numbers count once.
- N/A answers always stay at the end.
- Placement: the
sortcommand can go in any question, but the array must be filled by a question the respondent submits before the sorted question—for example, put[CustomOrder << 4,3,2,1]in Q1 to sort Q2.
💡 Tip: To sort answers alphabetically, you don't need an array: use
[sort by a-z Q1A]. See Survey Flow Operations Reference.
Reserved Variables
aytm fills some variables in for you, such as the respondent's device, browser, age, gender, and country. Pipe them the same way as your own variables:
We've noticed that you're using a [device] to take this survey. Is this correct? — a respondent on a phone sees "We've noticed that you're using a mobile to take this survey. Is this correct?"
In list surveys, [var1], [var2], and [var3] hold custom values passed in through the survey link. For the full list of reserved variables and their values, see Reserved Variables in the aytm Logic Reference Guide.