Retrieving variables

Retrieving top-level variables

To access a top-level variable, use the variable name. For example, the expression user will evaluate to the value of variable stored with name “user” in the root. So the following example prints the value stored in that variable:

${user}  

A top-level variable can be created in multiple ways. Its most important use is to retrieve fields from a personalization record.

If there is no such top-level variable defined in the personalization record or assigned with <#assign>, RPL will return an empty string.

In this expression, the variable name can contain only letters (including non-Latin letters), digits (including non-Latin digits), underline (_), dollar sign ($), at sign (@) and hash (#). Furthermore, the name must not start with digit.

Personalization fields and campaign variables appear as top level variables in RPL.  However, you can create additional higher level variables with the assign, local and global directives.

Retrieving data from a hash

If you have a hash as a result of an expression, you can get its sub-variable with a dot and the name of the sub-variable. Assume that you have this data model:

(root)
 |
 +- book
 |   |
 |   +- title = "Breeding green mice"
 |   |
 |   +- author
 |       |
 |       +- name = "Julia Smith"
 |       |
 |       +- info = "Biologist, 1923-1985, Canada"
 |
 +- test = "title"  

You can read the title with book.title, since the book expression will return a hash. Applying this logic further, you can read the name of the author with this expression: book.author.name.

Alternately, you can specify the sub-variable name with an expression: book["title"]. You can specify any expression that evaluates to a string in the square brackets, for example book[test].

The following examples are equivalent:

${book.author.name}
${book["author"].name}
${book.author.["name"]}
${book["author"]["name"]}

When you use the dot syntax, the same restrictions apply regarding the variable name as with top-level variables (name can contain only letters, digits, _, $, @, etc.). There are no such restrictions when you use the square bracket syntax, since the name is the result of an arbitrary expression. Note that if the sub-variable name is * (asterisk) or **, you do not have to use square bracket syntax.

Unlike top level variables, undefined variables within a hash do not return the empty string. Rather, if an attempt is made at retrieving a variable that does not exist, an error will result and RPL will terminate further processing, resulting in a launch failure. You can avoid susch failures with the use of the exclamation point operator (!), the double question mark operator (??), or <#attempt> and <#recover>.

Retrieving data from a sequence

This is the same as for hashes, but you can use only the square bracket syntax, and the expression in the brackets must evaluate to a number, not a string. For example, to get the name of the first animal of the example data model, use animals[0].name.

Retrieving special variables

Normally you do not need to use special variables.

Special variables are defined by the RPL. To access them, you use the .variable_name syntax.

Next steps

String operations

Working with variables

Handling missing variables