Using Advanced Expression Piping within your forms you can create mathematical formulas in order to calculate a value. Use INDEXOF to reference the location of a character/value within a string.
The INDEXOF function allows for two string values to be used and returns the index of the second value within the first value.
If "example" is the first value and we're looking for ‘x’ within it, the expression would return 1. e=(0), x=(1), a=(3), etc.
The index returned will only be the first instance of the value. Therefore, 'e' would return only 0 as the index even though there are two instances within the word "example" at 0 & 5.
The setup and formatting of the INDEXOF expression is imperative in preventing an error message from appearing on your form where the result of your expression would normally appear. The first line of the INDEXOF expression should be set up as follows:
{{ INDEXOF(variable,‘value’) }} |
TIP! The reference to variable in the expression above can be either the identifier of a question or the index of a field within a question.
To reference our previous example where we are locating the index of a value within the word "example", our expression could be:
{{ INDEXOF(example,'x') }} = 1 |
{{ INDEXOF(example,'e') }} = 0 |
{{ INDEXOF(example,'s') }} = -1 |
More commonly, the index of a value can be pulled from the response of a text response question or from a response of a specific cell within a grid.
INDEXOF a Text Response Question | {{ INDEXOF(q1,'a') }} | Will tell us the index of the value 'a' within question 1. NOTE: In this example, q1 has been used as the identifier of question 1. |
INDEXOF a single cell | {{ INDEXOF(q2[0],'@') }} | Based on Grid Indexing, this will tell us the index of the value '@' within the first cell of question 2. |
Consider a case where it is required to know the number of characters before the @ symbol within an email response field. The question where the email is requested from the applicant falls within a Single Column question with the identifier about and is the fifth subquestion within the grid question.
Identifier: | about |
Question reference: | about.4 |
Expression: | {{ INDEXOF(about.4, '@') }} |
User's response: | email@email.com |
Index returned: | 5 |
Extending on the previous example, a group of characters can be used within the expression. In these cases, the index of the first character within the group of characters stated within the expression will be the index given in the response.
Identifier: | about |
Question reference: | about.4 |
Expression: | {{ INDEXOF(about.4, '.com') }} |
User's response: | email@email.com |
Index returned: | 11 |
If identifying information is collected using a multi-column question as names, email addresses, phone numbers, etc., we would need to adjust the formatting of the expression in order to target a single cell within the grid.
Identifier: | about |
Question reference: | about[1] |
Expression: | {{ INDEXOF(about[1], '@') }} |
User response: | email@email.com |
Index returned: | 5 |