Loop "For each" Version 10 (Java)
Action group: Base actions
Deschemeion
This action is designed to perform a given iteration over all items in the collection. To add a sequence of actions to the loop body, you must move them to the "For each" action loop.
Action icon
Settings of patameters
Property | Deschemeion | Type | Filling example | Mandatory field |
Parameters | ||||
Сollection | A collection of objects that will be looped through | Robin.Collection | Yes | |
Results | ||||
Object | The object in which each element of the collection will be placed during iteration | Robin.Object |
Special conditions of use
Procedure for working with the "For each" loop
- Select the "For each" action from the "Base actions" action group and transfer it to the scheme.
- Fill the body of the loop with the necessary actions.
Example of filling: - Fill in the action parameters.
- Collection (list of values to be processed in the loop).
Example of filling parameters:
- Collection (list of values to be processed in the loop).
4. The result of the loop: the object into which each element of the collection will be placed during iteration execution.
A comparison of the "For" and "For each" loops.
The "For" loop will perform the operation the specified number of times. The iterator will take values from the initial specified value to the final value in increments specified in the "Increment" field.
The "For each" loop works sequentially with the elements of the selected collection. The number of iterations is equal to the number of elements in the collection selected on the input.
Thus, the result of the "For" loop is a number, and "For each" is an object.
Example of use
Task
There is a collection of variables of type "Number" containing the value "(1988, 8, 2, 4)".
The loop must be terminated until the value of the number variable is equal to the value "2".
Solution
- Add a "For each" loop.
Add an "Equal to" action.
Add "If" condition. In the "True" branch add a message to the log and loop interrupt.
Implementation
Create a variable with the type "Collection". Enter values (1988, 8, 2, 4) of the "Numeric" type into the collection.
Move the Loop "For each" action to the Studio workspace.
"Collection" parameter. Specify the created collection "Collection". During the work of the robot, the loop will run through each of the values in the collection.
Move the "Equal to" action to the studio workspace, inside the "For each" loop.
Parameter "First operand". In this parameter we specify the object of the current iteration of the "For each" loop.
Parameter "Second operand". In this parameter we specify the number "2". If the number 2 is found in the "For each" loop, the loop will finish its work.
Move the "If" action to the studio workspace, inside the "For each" loop.
The "Condition" parameter. This parameter specifies the result of the "Equal to" action. If the result of the "Equal to" action is true, the robot will follow the "True" branch, if it is false, it will follow the "False" branch.
In the "Truth" branch of the "If" action, move the "Message in log" action.
In the "Truth" branch of the "If" action, move the "Break the loop" action.
Launch the robot using the "Start" button in the top panel. The final scheme of the robot will look as follows:
Result
The program robot completed successfully. The algorithm went through the loop and was interrupted after the value "2" was found in the collection.
Case study "Sorting". Loop "For each"
Task
The folder contains files with the extensions .xls(x) and .docx. You need to create folders with the name of the extension and distribute the files into folders.
Solution
Move the "Folder list" and "For each" actions to the workspace.
Fill in the action parameters.
"Folder list" action.
In the "Folder" line, specify the path to the folder with the files to be sorted.
In the "Objects type" field specify the value "Only files".Action "For each".
In the "Collection" line, specify the result of the "Folder list" action.
Place the following actions in the loop body in the order specified:
"Get file info";
"Extract by position";
"Search directories";
"Get length";
- "Greater than";
"If";
On the "True" branch, move the actions:
"Get index of element";
"Convert data";
"File move";On the "False" branch, move the actions:
"Folder create";
"File move".
Fill in the action parameters.
For the "Get file info" action:
The "File" parameter is a reference to the object being processed at the time of loop iteration.For the "Extract by position" action;
The "Source text" parameter is the result of the "Extension" of the "Get file info" action.
Parameter "Position" - 1.
Parameter "Length" - if this parameter is not filled in, the text is extracted with the specified position to the end.For the "Search directories" action.
The "Start directory" parameter - the path to the folder where the subfolder for sorting files is stored.
The "Match pattern" parameter - the result of the "Extract by position" action.For the "Get length" action.
The "Collection or array" parameter is the result of the "Search directories" action.For the "If" action.
Use the expression construction tool to write the boolean operator "Greater than" into the condition parameter, where the first operand is the result of the "Get length" action and the second operand is 0.For the "Get index of element" action.
The "Collection" parameter is the result of the "Search directories" action.
The "Index" parameter is 0.For the "Convert data" action.
The "Source" parameter is the result of the "Get index of element" action.
The "Data type" parameter is the path to the folder.For the "File move" action.
The "File" parameter is a reference to the object processed at the moment of loop iteration.
The "Target folder" parameter is the result of the "Convert data" action.For the "Folder create" action.
The "Parent folder" parameter is the path to the folder where the subfolder for sorting files is stored.
The "Name" parameter is the result of the "Extract by position" action.For the "File move" action.
The "File" parameter is a reference to the object processed at the moment of loop iteration.
The "Target folder" parameter is the result of the "Folder create" action.
Result
The program robot completed successfully.
The algorithm went through the loop, sorting the files into folders.