Methods for Entity Manipulations
Working with Variables
As you already know, the variables are context-independent and therefore their values persist throughout the digger's entire operation. This fact can be used to unite data disparate in different documents into a single object. To do it, you have to parse one document, save the data in a variable, and when parsing another document, read the veriable value to the register and write them to the object. Also, variables are often used to substitute values into requests or CSS selectors.
The variable_set command allows you to set the value of a variable. It can be used in any context, but if the command notation implies the use of a register value, then this notation can only be used in a block context only.
Please note:
If the register is empty and you are trying to write this empty value into a variable, the command will be ignored. This is done in order to work out the cases when you need to store the previous value of the field if the field is missing in subsequent cycles.
To erase a variable, use the variable_clearcommand.
The following are various examples of writing values to variables:
# JUMP TO BLOCK CONTEXT
- find:
path: .somepath
do:
# PARSE TEXT
- parse
# SAVE VALUE OF THE REGISTER TO VARIABLE WITH NAME `somevar`
- variable_set: somevar
# JUMP TO BLOCK CONTEXT
- find:
path: .somepath
do:
# PARSE TEXT
- parse
# LETS IMAGINE THAT IN REGISTER NOW: 123
# SAVE VALUE OF THE REGISTER TO THE VARIABLE WITH NAME `somevar`
- variable_set: somevar
# JUMP TO OTHER BLOCK
- find:
path: .anotherpath
do:
# PARSE TEXT
- parse
# SAVE REGISTER VALUE TO THE VARIABLE
# NAME OF VARIABLE IS RESULT OF CONCATENATION OF `another_` AND VALUE OF VARIABLE `somevar`
- variable_set: another_<%somevar%>
# SO VARIABLE NAME WILL BE `another_123`
- walk:
to: http://www.somesite.com/
do:
# SET VALUE FOR VARIABLE
# IF YOU ARE NO IN BLOCK CONTECT, ONLY SUCH NOTATION CAN BE USED
- variable_set:
field: somevar
value: 123
# YOU CAN ALSO USE VARIABLES FOR SUBSTITUTION IN VALUE AND FIELD
# BELOW LOGIC WILL ASSIGN VALUE "123" TO THE VARIABLE WITH NAME `another_123`
- variable_set:
field: another_<%somevar%>
value: <%somevar%>
The variable_get command is used to write the value of a variable to the register.
The variable_append and variable_prepend commands can be used for adding
the content of the variable to the end or beginning of the register.
To clear the contents of a variable, you can use the variable_clear command.
It is useful when iterating over blocks with unstable structure, when some element may be missing and the variable_set command
will not be executed. In this case the old value will remain in the variable, and can be mistakenly written to the object.
If you need to avoid it, you can clear the contents of the variable with this command.
# JUMPING TO THE BLOCK
- find:
path: .somepath
do:
# READING VALUE OF THE VARIABLE `somevar` TO THE REGISTER
- variable_get: somevar
# SET VARIABLE `somevar` WITH VALUE
- variable_set:
field: somevar
value: 123
# SET VALUE OF VARIABLE `another_123`
- variable_set:
field: another_123
value: 456
# JUMPING TO THE BLOCK
- find:
path: .somepath
do:
# READING VALUE OF VARIABLE `another_123` TO THE REGISTER
- variable_get: another_<%somevar%>
# REGISTER VALUE: 456
# SET VALUE OF VARIABLE `somevar`
- variable_set:
field: somevar
value: 123
# JUMPING TO THE BLOCK
- find:
path: .somepath
do:
# SET REGISTER VALUE TO `data` LITERAL
- register_set: data
# APPEND VALUE OF THE VARIABLE `somevar` TO THE REGISTER VALUE,
# USING " - " FOR JOINING VALUES
- variable_append:
field: somevar
joinby: " - "
# REGISTER VALUE: data - 123
# PREPEND VALUE OF THE VARIABLE `somevar` TO THE REGISTER VALUE,
# USING "" FOR JOINING VALUES
- variable_prepend:
field: somevar
joinby: ""
# REGISTER VALUE: 123data - 123
# CLEAR VARIABLE `somevar`
- variable_clear: somevar
Next we will tell you how you can work with arguments.