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:

SituationFormulaResultComment
A vector of numbers is supplied[1, 2, 3]*55Only the first element of the vector (1) is considered
A vector of strings is suppliedGETvariable(["gender", "age"])maleOnly 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 to undefined.
  • 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) returns 25.
  • 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 needle or stack is undefined (or any element of theirs is undefined), the result of the operator is undefined.
  • If either needle or stack are strings (or vectors of strings), both operands are converted to strings (or vectors of strings).
  • If either needle or stack are 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 needle is a vector, the operator returns TRUE if at least one element of the needle vector is present in the stack vector.

Examples:

FormulaResultComment
2 @ [1, 2, 3]TRUE2 is present in the vector
4 @ [1, 2, 3]FALSE4 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)TRUE2 is converted to “2” and is present in the vector
c(1, 2, 3) @ c(4, 5, 2)TRUE2 is present in both vectors
c(1, 2, 3) @ c(4, 5, 6)FALSENo common elements in the vectors
c(TRUE, FALSE) @ TRUETRUEThe TRUE from the needle is present in the stack vector
c(TRUE, FALSE) @ 5FALSEc(TRUE, FALSE) is converted to [1, 0] and neither 1 nor 0 is present in the stack 5
c(TRUE, FALSE, "5") @ 5TRUEc(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