# How to loop over an array in logstash

**URL:** <https://discuss.elastic.co/t/how-to-loop-over-an-array-in-logstash/153781>\
**Category:** Logstash\
**Created:** [October 24, 2018, 11:21am UTC](https://discuss.elastic.co/t/how-to-loop-over-an-array-in-logstash/153781 "2018-10-24T11:21:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![moshikoy](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@moshikoy](https://discuss.elastic.co/u/moshikoy)\
**Post date:** [October 24, 2018, 11:21am UTC](https://discuss.elastic.co/t/how-to-loop-over-an-array-in-logstash/153781/1 "2018-10-24T11:21:45Z")

</div>

Hi,  
I was wondering what is the correct syntax for what i'm trying to achieve.  
I'm splitting a field using a separator, the value may be splitted to 2 or more sub-fields.  
I'd like to put these sub-fields in new fields if they exists (basically I need to loop over the array)

How can I achieve it?

Example  
Input: cron\_id =\> "1\_2\_3"  
output:  
cron\_l1 =\> 1  
cron\_l2 =\> 2  
cron\_l3 =\> 3  
cron\_l4 =\> "" (EMPTY)

Logstash Code:

> mutate {  
> split =\> { "cron\_id" =\> "\_" }  
> add\_field =\> {  
> "cron\_l1" =\> "%{[cron\_id][0]}"  
> "cron\_l2" =\> "%{[cron\_id][1]}"  
> "cron\_l3" =\> "%{[cron\_id][2]}"  
> "cron\_l4" =\> "%{[cron\_id][3]}"  
> }  
> }  
> if [message] =~ "^#" { drop{} }  
> }

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 21, 2018, 11:36am UTC](https://discuss.elastic.co/t/how-to-loop-over-an-array-in-logstash/153781/2 "2018-11-21T11:36:27Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
