In some situations it is convenient to have multiple individual elements (numbers, texts, variables) to be combined in a single collection - an array. Once an array is created, whether the same for everyone, or different for each respondent, it can be used in many scenarios:
- Extract individual elements by their indices
- Display the whole array to the respondent, each element delimited with a comma
- Compare to another array and see if they match or not
- Do an exact search within an array. For example, you might want to find if respondent said "agree" or "yes" in any of his OE responses (previously collected into an array)
- Search for a part of text in an array. Useful for checking if respondent mentioned specific brand in his OE response, given an array of possible misspells of the brand
Syntax
- [ar << 1] Create new array with number 1, or add to existing array number 1
- [ar << 1,2,3,4] Create new array with numbers 1,2,3,4 , or add to existing array numbers 1,2,3,4
- [ar_1 << "first", "second", "third"] Create new array or populate an existing one with texts "first", "second", and "third"
- [v4 << '0', 1, 2, 3] multiple types in an array are supported
- [ar3|3] pipe in third item from ar3
- [ar3] pipe whole array, comma delimited
- [ar4|3 + 1] add 1 to the third item in ar4
- [show if ar4|501 = 7] shown if 501th item in ar4 equals 7
- [show if ar3 = ar4] shown if arrays are equal to each other (order of items matters)
- [show if "first" in ar_1] shown if there's at least one "first" in the ar_1
- [show if "st" like ar_1 ] shown if there's at least one text that contains "st" in ar_1
|
Examples
- You asked respondents where their most exciting vacation took place in an open-ended question. Further, if respondent mentioned a variation of the US country name, you want to ask additional questions. At the beginning of the survey, in a section that every respondent would see you would put [usvariants<<"US","U.S.","United States","USA","U.S.A.","United States of America"] array declaration. This array can now be used in the section of questions to only show them if US was mentioned in the OE: [show if Q5RC like usvariants]
- During a survey you assign respondents labels, such as "early bird", "influencer", etc. At the end of the survey you want to show some questions to each labeled group, e.g. only "early bird" respondents would see Q10, only "influencers" would see Q11, and so on. While a respondent can be profiled to multiple labels at once, you would require multiple individual variables ([earlybird=1] if respondent is an early bird, [influencer=1] if respondent is an influencer, etc). A solution with arrays is much more elegant. You would use an array named "labels". During the survey, if respondent satisfies the profiling criteria, you would add the label to the array with [labels<<"influencer"]. At Q10 you would simply write [show if "early bird" in labels], and in Q11 [show if "influencer" in labels].
|
Shuffle
An array can be shuffled, meaning positions of its elements get randomly redistributed. Useful when there's a need to randomize the order at which the items are shown from the pile of all possible variations. An optional modifier "max" allows limiting the length of the resulting array, so that only the specified number of random elements is returned.
Syntax:
- [scores_array<<1,2,3,4,5] [scores_array = shuffle scores_array] shuffle first five natural numbers
- [scores_array = shuffle scores_array] shuffle scores_array
- [arr2 = shuffle arr1] shuffle arr1 and save into arr2
- [scores_array<<1,2,3,4,5] [scores_array = shuffle scores_array max 2] shuffles first five natural numbers and saves into scores_array only two of them
- [scores_array = shuffle scores_array max 2] shuffles scores_array but saves only two elements
|
Reverse
An array can be reversed - the last element takes place of the first, the second last of the second first, and so on.
Syntax:
- [scores_reversed = reverse scores] Reverse scores array and store into scores_reversed
|
Unique
An array can be shortened by removing all duplicates, thus ensuring that each element is unique in the resulting set.
Syntax:
- [scores_unique = unique scores] Only store unique elements from scores into scores_unique
|
Sort
Sort an array in ascending direction. The behavior is different depending on the type of array:
- If the array is numerical (all members are numbers), the sorting will be ascending numerical, 1 < 10 < 11 < 100.
- If the array is character (some members are strings), the sorting will be ascending, where numerals precede letters, capital letters precede lower-case letters. E.g. "1" < "10" < "100" < "11" < "Apples" < "apples".
Syntax:
- [scores_sorted = sort scores] Sort scores and store into scores_sorted.
- [scores_sorted_descending = reverse sort scores] Sort scores in descending order and store into scores_sorted_descending.
|
Sort elements by array
Arrays can be used to impose a specific order in which elements in the survey will be shown. An array of one-based indices applied over a set of items (for example, over sub-questions of the Slider question), will instruct the system to place said items in the specified order. The location of the logic does not matter, it may be placed on any question in the survey (still, make sure the variable by which the sorting happens is initialized before the sorted question). Note simpler approach for alphabetical sorting: sort elements a-z.
Syntax:
- [CustomOrder << 4, 3, 2, 1][sort q2a by CustomOrder] Assuming Q2 has four elements, the elements will be shown in reversed order to respondents. The first number 4 means that the first element in Q2 has to go to the fourth position.
- [CustomOrder << 4, 3, 2, 1][sort q2a by CustomOrder if gender='m'] Assuming Q2 has four elements, the elements will be shown in reversed order to female respondents, while male respondents will see the original order.
- [CustomOrder << 4, 3, 2, 1][sort q2sq by CustomOrder] Assuming Q2 has four sub-questions, the sub-questions will be shown in reversed order to respondents. The first number 4 means that the first sub-question in Q2 has to go to the fourth position.
- [CustomOrder << 4, 3, 2, 1][sort q2a by CustomOrder] Assuming Q2 has five elements, the elements will be shown in reversed order to respondents, except of the fifth element, which will remain fifth because it didn't receive instructions to go anywhere, and the fifth location is the only one open after all previous operations have been performed.
- [CustomOrder << 1, 2, 3, 4] [CustomOrder=shuffle CustomOrder] [sort q2a by CustomOrder] [sort q3a by CustomOrder] Assuming Q2 and Q3 have four elements each, the elements will be randomized, and shown in the same order in Q2 and Q3.
|