Rules for using variables in expressions

<< Click to Display Table of Contents >>

Navigation:  Several practical recommendations > Using Variables in Tasks >

Rules for using variables in expressions

Using variables in actions permits to achieve more flexibility of an algorithm. Sometimes you just can’t do without using variables. For example, in cycles.

If you use expressions with variables in parameters of actions, RoboTask at first expands all variables, and only after that uses the result for the action. In other words, RoboTask computes the variable value and substitutes the value for the variable in the expression.

For example, the expression

Temporary file is {UniqueFileName({TempDir}\*.tmp)}

will approximately have the following form after its computation:

Temporary file is C:\Users\User1\AppData\Local\Temp\DA0PUY08.tmp

 

The rules for computing values are simple:

The rule of visibility. At first, RoboTask tries to find the variable in the list of local variables. Then, if it fails, it looks up among global ones. If the variable isn’t found, substitution doesn’t occur. System variables are always global. Inherently they are functions which return the result of some computations. This rule is described in the unit "Local Variables".

The depth of expanding variable.  If variables are used in an expression by means of braces {}, the variables are expanded to the whole depth. That is, firstly the value of variable is computed, and then the result is expanded if it contains variables. And so on, to the end. However, sometimes it is necessary to expand only the specified variable without further recursive processing. To do this you have to specify variables with the character # after an opening brace: {#MyVariable}. More details in this unit  below.

 

By default, RoboTask tries to expand all variables in the specified expression. Then it recursively reiterates the attempt to expand variables in resultant expression until all variables are expanded. However, sometimes it is necessary to protect a text from expanding variables. These can be some technical texts for automated processing. These texts aren’t directly related to RoboTask. They can contain inserts coinciding with the variables of RoboTask. The use of braces is quite a widespread method of inserting some macros when texts are processed automatically.

 

For example, there is such a text file:

To automate mailing you can use macros to create a message:

insert of a customer address - {Email}  

insert of a customer name - {User}

insert of a sender name - {SenderEmail}

insert of a current date into the message - {Date}

insert of current time into the message - {Time}

insert of a signature - {Signature}

 

You have to process this text in a cycle together with other files by means of RoboTask. For example, add HTML tags and save it as HTML file preserving all the file contents without changing. It is easy to see that if we try to write the file contents into a variable by means of a system variable {TextFile(file_name)}, expressions {Date} and {Time} will be replaced with current meanings of date and time.

To save a text to a variable without changing, you have to use the symbol # after the opening brace: {#TextFile(file_name)}. In this case RoboTask doesn’t try to expand all variables in the text recursively, but only returns the contents of the text file without changing. Naturally, you also have to use the variable containing the text with the symbol #, in order to prevent from expanding expressions in text. For example: {#CurrentText}.

 

note Related Topics

Using Variables

System variables

Custom variables

Local variables

Create a Variable

Add Variables to a Task

Saving of a result into a variable

How to use variables correctly

Options > Variables