Entity Manipulations

Counters

Counters can be useful in situations when you need to count certain events. For example, to count the number of iterations of the walk command in repeat mode. Counters can be used for values substitution, so you can use them to solve pagination tasks, and not only to use it as page number, but also as record number, because you can increase (or decrease) value of counter by any integer number.

Before using the counter, you can initialize it with any integer value using counter_set command. If the counter has not been initialized explicitly, it will be set to 0 when digger execute one of the following commands: counter_get, counter_increment or counter_decrement.

All methods supposed to work with counters, with the exception of counter_get (only works in a block context), can be used in all contexts.

Let's overview the usage of counters in more details on examples:

              # CREATE BLOCK FROM HTML STRING
- register_set: '<div>123</div>'
- to_block
- find:
    path: div
    do:
    # PARSE TO THE REGISTER
    - parse

    # INITIALIZE COUNTER  `somecounter` WITH VALUE OF THE REGISTER (123)
    - counter_set: somecounter
    # COUNTER VALUE: 123

    # ------------------------------------------------------
    # SET REGISTER TO SOME INTEGER VALUE
    - register_set: 456

    # INITIALIZE COUNTER `somecounter` WITH VALUE OF THE REGISTER (456)
    - counter_set: somecounter
    # COUNTER VALUE: 456

# ------------------------------------------------------
# INITIALIZE COUNTER `somecounter` DIRECTLY
- counter_set:
    name: somecounter
    value: 100
# COUNTER VALUE: 100
            
              # CREATE BLOCK FROM HTML STRING
- register_set: '<div>232</div><p>block</p>'
- to_block
- find:
    path: div
    do:
    # PARSE TO THE REGISTER
    - parse

    # SET COUNTER WITH NAME `somecounter` WITH VALUE OF THE REGISTER
    - counter_set: somecounter
    # COUNTER VALUE: 232

- find:
    path: p
    do:
    # READ COUNTER `somecounter` VALUE TO THE REGISTER
    - counter_get: somecounter
    # REGISTER VALUE: 232

    # YOU CAN ALSO USE COUNTERS FOR VALUES SUBSTITUTION, EG WHEN YOU USE register_set COMMAND:
    - register_set: <%somecounter%> persons
    # REGISTER VALUE: 232 persons
              
              # CREATE BLOCK FROM HTML STRING
- register_set: '<div>232</div><p>block</p>'
- to_block
- find:
    path: div
    do:
    # PARSE TO THE REGISTER
    - parse
    # REGISTER VALUE: 232

    - counter_set: somecounter
    # COUNTER VALUE: 232

- find:
    path: p
    do:
    # INCREMENT COUNTER BY 1
    - counter_increment: 
        name: somecounter
    # COUNTER VALUE: 233

    # INCREMENT COUNTER BY 24
    - counter_increment: 
        name: somecounter
        by: 24
    # COUNTER VALUE: 257
              
              # CREATE BLOCK FROM HTML STRING
- register_set: '<div>232</div><p>block</p>'
- to_block
- find:
    path: div
    do:
    # PARSE TO THE REGISTER
    - parse

    # INITIALIZE COUNTER `somecounter` WITH THE VALUE OF THE REGISTER
    - counter_set: somecounter
    # COUNTER VALUE: 232

- find:
    path: p
    do:
    # DECREMENT COUNTER BY 1
    - counter_decrement: 
        name: somecounter
    # COUNTER VALUE: 231
    
    # DECREMENT COUNTER BY 24
    - counter_decrement: 
        name: somecounter
        by: 24
    # COUNTER VALUE: 207
              
              # CREATE BLOCK FROM HTML STRING
- register_set: '<div>232</div><p>block</p>'
- to_block
- find:
    path: div
    do:
    # PARSE TO THE REGISTER
    - parse

    # INITIALIZE COUNTER `somecounter` WITH THE VALUE OF THE REGISTER
    - counter_set: somecounter
    # COUNTER VALUE: 232

- find:
    path: p
    do:
    # RESET COUNTER
    - counter_reset: somecounter
    # COUNTER VALUE: 0
              

In the next chapter, we'll see what methods can be used to work with the link pools.