Task: Add the 4th element to the collection, setting its value as the sum of the values of the 1st and 2nd elements in the collection.

This is already a complex expression, and before implementing it, let’s create a plan for its realization:

f1(f2(c, i1), f2(c, i2))

where:

  • f1 = + - addition operation;
  • f2 - action «Get value by index» from the group «Collections»;
  • c - collection at the step «Add element 2» or later;
  • i1 = 0 - the index of the 1st element in the collection (value of the element is 5);
  • i2 = 1 - the index of the 2nd element in the collection (value of the element is 22).

As a result, the 4th element in the collection should have a value equal to 27.

Let’s follow the steps to implement the expression:

  1. Add a new element to the collection using the action «Add element».

  2. Set the parameter «Collection» for the action «Add element 4». Click on the button with three dots, and then select «Add element 3» from the robot’s actions. 

  3. Set the parameter «Value» for the action «Add element 4». Click on the button with three dots, and then click the button - call the expression constructor.

  4. In the pop-up window, select: «Operations/Arithmetic/Addition» and double-click the implementation of the action using the left mouse button:

  5. Additional fields for setting operand values will appear in the upper-left corner:

  6. In the field of the first operand of the operation «Addition», we need to extract the first element from the collection. To do this:

  • Select the field of the first operand by placing the mouse cursor either to the left or right of the field and left-click it once - the field will be selected with blue stripes on the left and right sides of it

  • Choose the action «Get value by index» from the group «Collections»

  • Double-click on the implementation of the action to get the panel for setting the parameters of the action «Get value by index 1»

  • Set the collection for the first operand of the action «Get value by index 1» through the button with three dots, and then the button indicated in the screenshot, and select the resource variable C from the robot actions

  • Set the index for the first operand of the action «Get value by index 1» as 0

  • We get the ready first operand of the operation «Addition»:

7. Repeat the same steps for the second operand of the operation «Addition»:

But this time, set the index of the collection to 1:

8. After entering the operand values for the operation «Addition», click the «Save» button and save the result in the resource variable C. The fourth element is added.

9. A new artifact «Expression_Add element 4_Value» appeared on the robot scheme.

10. Run the robot and check the result:

We can see that the collection now has the 4th element, and its value is 20 - the sum of the 1st and 2nd elements in the collection, set earlier.

  • Нет меток