Working with vectors
The formula language also supports vectors, which are lists of values of the same type. They are created using square brackets (e.g. [1, 2, 3] or ["a", "b", "c"]).
If a vector is supplied where a single value is expected, only the first element of the vector is considered:
| Situation | Formula | Result | Comment |
|---|---|---|---|
| A vector of numbers is supplied | [1, 2, 3]*5 | 5 | Only the first element of the vector (1) is considered |
| A vector of strings is supplied | GETvariable(["gender", "age"]) | male | Only the first element of the vector ("gender") is considered |
But some functions, like concat, max, min, c, are designed to work with vectors and will process all elements of the vector.
When mixed data types are supplied in a vector, the formula language will try to convert all elements of the vector to a single type:
- If any element is
undefined, all values will be converted toundefined. - If any element is a string and there are no undefined elements, all values will be converted to strings.
- If any element is a number and there are no strings and no undefined elements, all values will be converted to numbers.
A vector is displayed in a survey (and passed in redirects) as a comma-separated list of values. For example, [1, 2, 3] is displayed as 1,2,3.
c
The function c(x) can be used to form a vector. It is equivalent to the [...] notation. It expects at least one argument.
Signature: c(x, ...)
Examples:
c(25)returns25.c(12,123,"23")returns["12","123","23"].
Not yet implemented: The operator @
The operator @ is used to check if a value is present in a vector. It returns TRUE if the value is present and FALSE if it is not.
Signature: needle @ stack, where needle is the value to check and stack is the vector to check in.
Both needle and stack can be of any type. The following rules apply:
- If either
needleorstackis undefined (or any element of theirs is undefined), the result of the operator isundefined. - If either
needleorstackare strings (or vectors of strings), both operands are converted to strings (or vectors of strings). - If either
needleorstackare numbers (or vectors of numbers) and there are no strings and no undefined values, both operands are converted to numbers (or vectors of numbers). - If
needleis a vector, the operator returnsTRUEif at least one element of theneedlevector is present in thestackvector.
Examples:
| Formula | Result | Comment |
|---|---|---|
2 @ [1, 2, 3] | TRUE | 2 is present in the vector |
4 @ [1, 2, 3] | FALSE | 4 is not present in the vector |
"b" @ ["a", "b", "c"] | TRUE | “b” is present in the vector |
"d" @ ["a", "b", "c"] | FALSE | |
2 @ c(1, "2", 3) | TRUE | 2 is converted to “2” and is present in the vector |
c(1, 2, 3) @ c(4, 5, 2) | TRUE | 2 is present in both vectors |
c(1, 2, 3) @ c(4, 5, 6) | FALSE | No common elements in the vectors |
c(TRUE, FALSE) @ TRUE | TRUE | The TRUE from the needle is present in the stack vector |
c(TRUE, FALSE) @ 5 | FALSE | c(TRUE, FALSE) is converted to [1, 0] and neither 1 nor 0 is present in the stack 5 |
c(TRUE, FALSE, "5") @ 5 | TRUE | c(TRUE, FALSE, "5") is converted to ["1", "0", "5"] and 5 is converted to "5" |
"Big" @ topLevel(123,2)(for a respondent who was only choosing the “Big” level in that attribute) | TRUE | “Big” is present in the vector of top 2 levels for attribute with ID 123 |
["Big","Medium"] @ topLevel(123,2)(for a respondent who was only choosing the “Big” level in that attribute) | TRUE | “Big” is present in the vector of top 2 levels for attribute with ID 123 |