Expressions are used extensively in SurveyEngine as a means to:
- control flow of the survey – through branching
- decide whether or not to display an element – through conditions
- decide whether to activate an element – such as segment assignment
- decide whether some user input is valid – in validations
- generate a new value in the data – with derived values
- pipe values into the displaying
The default expression language in SurveyEngine is Perl. For simple expressions this is very similar most popular lanuages including C, VB, javascript and some excel functions. All expression examples in this section are in the Perl syntax.
What are expressions?
Expressions are mathematical or logical constructs that generate an output.
A simple expression is
1+1
which produces the output '2'
Terminology
In order to talk about expressions we have to introduce some terminology.
'Literal' - this is a number or a string. It has the value that is stated. The numbers 1, 2 and 3 are literals. So is the string 'hello world'. Strings must be quoted with single quotes.
Variable – this is a holding name for a container that could have any literal value. In your survey, all the names given to response items are variables. The actual value contained depends on what the user has entered or clicked on.
Variables have a dollar sign in front of them:
$x, $y and $question1 are all variable names
Operator – this is a symbol that does something to a variable, or literal to change it in a meaningful way.
The symbols +, -, / and * are all operators. More interesting operators are 'if', '==' (equals), eq
Syntax – as spoken languages have rules regarding how nouns, verbs and conjunctions are assembled, so too to expressions. Syntax describes the rules governing how expressions are built.
Expressions – are assemblies of variables, literals and operators – in a structured way – that produce an output
Boolean values - in an abstract way boolean values can only be true or false. To represent these numerically, a zero, an empty string or undefined value is false. Anything else is true.
Boolean operators - the set of operators that perform a true/false test on values. They always output a 1 for true or a 0 for false.
More details on all of these are provided later, but to get a feeling for expressions some examples are provided below.
Expression | Output | |
1+2 | 3 | |
1==2 | 0 | This is an example of a boolean test. It could be read “does 1 equal 2”, to which the output is 'No'. In perl a False is represented as a zero |
1!=2 | 1 or true | Reverse or above. Is 1 not the same as 2, to which the output is 'Yes' or True, represented as a 1. |
'cat' eq 'dog' | 0 or false | A similar test to above. In this case we are comparing strings. Note that we should use the operator 'eq' |
'cat' ne 'dog' | 1 or true | |
$answer eq 'cat' | 1 if $answer is 'cat' 0 if otherwise |
A boolean test to check the value held in the variable $answer. Note a dollar sign indicates that answer is a variable. |
$answer eq 'cat' or $answer eq 'dog' | 1 if $answer is either 'cat' or 'dog' 0 otherwise |
Joining two expressions with an 'or' |
$state eq 'nsw' and $postcode eq '2026' | 1 only if state is nsw and the postcode is 2026 0 otherwise |
|
'goodbye'.' cruel world' | 'goodbye cruel world' | Joining two strings with the dot operator |
$answer1.'-'.$answer2 | (will create a new string from the values in answer1 and answer2 with a hypen between them | The same as above but with variables. |
If ($answer eq 'nsw') {'New South Wales'} elsif($answer eq 'sa') {'South Australia'} else {'somewhere else'} |
Will output the full names of the states for nsw and sa, otherwise the text 'somewhere else' | A conditional if-then-else statement, used when a specific output logic is required |
0 Comments