# For Each

{% hint style="info" %}
See the changelog of this Action type [here](https://app.gitbook.com/s/IXj83By4f20oCrZZ0gyP/for-each).
{% endhint %}

## Overview

The **For Each** action divides a list field with different entries into different output events, along with the position they occupy in the list (being the first position `0`).

For example, an input list containing `[a,b,c]` will generate three outputs, with these fields added to the event:

* `elementValueOutField: a`; `elementIndexOutField: 0`
* `elementValueOutField: b`; `elementIndexOutField: 1`
* `elementValueOutField: c`; `elementIndexOutField: 2`

<figure><picture><source srcset="https://965373739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkxZeV4nlXcIAjMGZxzLI%2Fuploads%2FP9c4CPBMkJTjf4k4c7Fs%2F2025-01-23_15-57-25.png?alt=media&#x26;token=7b9a0386-2014-4d16-92ae-b2c46be36340" media="(prefers-color-scheme: dark)"><img src="https://965373739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkxZeV4nlXcIAjMGZxzLI%2Fuploads%2FWrOlTn8DEutIFOA2xgjv%2F2025-01-23_15-49-19.png?alt=media&#x26;token=c129b143-6fdf-4381-b6f3-264af7c0443b" alt=""></picture><figcaption></figcaption></figure>

{% hint style="warning" %}
In order to configure this action, you must first link it to a Listener. Go to [Building a Pipeline ](https://docs.onum.com/the-workspace/pipelines/building-a-pipeline)to learn how this works.
{% endhint %}

## Ports

These are the input and output ports of this Action:

<details>

<summary>Input ports</summary>

* **Default port** - All the events to be processed by this Action enter through this port.

</details>

<details>

<summary>Output ports</summary>

* **Default port** - Events are sent through this port if no error occurs while processing them.
* **Error port** - Events are sent through this port if an error occurs while processing them.

</details>

## Configuration

{% stepper %}
{% step %}
Find **For Each** in the **Actions** tab (under the **Advanced** group) and drag it onto the canvas.
{% endstep %}

{% step %}
To open the configuration, click the Action in the canvas and select **Configuration**.
{% endstep %}

{% step %}
Enter the required parameters:

<table><thead><tr><th width="234">Parameter</th><th>Description</th><th data-hidden></th></tr></thead><tbody><tr><td><strong>Input</strong><mark style="color:red;"><strong>*</strong></mark></td><td>Choose the field that contains the list you want to divide.</td><td></td></tr><tr><td><strong>Output field</strong><mark style="color:red;"><strong>*</strong></mark></td><td>Name of the new field where each iterated element will be stored. This will be the same type as the input field list.</td><td></td></tr><tr><td><strong>Index field</strong><mark style="color:red;"><strong>*</strong></mark></td><td>Name of the new field that will show the position of each element in the list.</td><td></td></tr></tbody></table>
{% endstep %}

{% step %}
Click **Save** to complete the process.
{% endstep %}
{% endstepper %}

## Example

Imagine you receive a list-type field containing a string of five IPs:

`127.0.0.1`,`127.0.0.2`,`127.0.0.3`,`127.0.0.4`,`192.168.0.1`

{% stepper %}
{% step %}
Add the **For Each** Action to your Pipeline and link it to your required Data sink.
{% endstep %}

{% step %}
Now, double-click the **For Each** Action to configure it. You need to set the following config:

<table><thead><tr><th width="227">Operation</th><th>Parameters</th><th data-hidden></th></tr></thead><tbody><tr><td><strong>Input</strong></td><td>In this case, we choose the field <code>ipsList</code> that contains our IP list.</td><td></td></tr><tr><td><strong>Output field</strong></td><td>We're naming the new field <code>ipValue</code>.</td><td></td></tr><tr><td><strong>Index field</strong></td><td>We're naming the new field <code>ipIndex</code>.</td><td></td></tr></tbody></table>
{% endstep %}

{% step %}
Click **Save** to apply the configuration.
{% endstep %}

{% step %}
Now link the *Default* output port of the Action to the input port of your Data sink.
{% endstep %}

{% step %}
Finally, click **Publish** and choose in which clusters you want to publish the Pipeline.
{% endstep %}

{% step %}
Click **Test pipeline** at the top of the area and choose a specific number of events to test if your data is transformed properly. Click **Debug** to proceed.
{% endstep %}
{% endstepper %}

The Action will create a separate event for each element of the string, each event containing two fields (value and index).

<figure><img src="https://965373739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkxZeV4nlXcIAjMGZxzLI%2Fuploads%2FmmB6aOMl3lC6gTfMNFhd%2Fdark-medium%20(35).png?alt=media&#x26;token=5ef26090-eec2-4f2f-b5bb-c525f63a2cf6" alt=""><figcaption></figcaption></figure>
