# Loop over array in output

**URL:** <https://discuss.elastic.co/t/loop-over-array-in-output/151997>\
**Category:** Logstash\
**Created:** [October 11, 2018, 7:09am UTC](https://discuss.elastic.co/t/loop-over-array-in-output/151997 "2018-10-11T07:09:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drnjst](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@drnjst](https://discuss.elastic.co/u/drnjst)\
**Post date:** [October 11, 2018, 7:09am UTC](https://discuss.elastic.co/t/loop-over-array-in-output/151997/1 "2018-10-11T07:09:37Z")

</div>

Hello currently I have code that works, but could be improved if I would be able to iterate.  
Now I say if the 3rd element of the array exists, output the 3rd element (into a file).  
See code below:

```
output {
  if [value][3] {
    file {
      path => "\\host123\input\file.txt"		
      codec => line { format => "name: '%{name[3]}' value: '%{value[3]}'" }
    } 
  if [value][2] {
    file {
      path => "\\host123\input\file.txt"		
      codec => line { format => "name: '%{name[2]}' value: '%{value[2]}'" }
    } 
  if [value][1] {
    file {
      path => "\\host123\input\file.txt"		
      codec => line { format => "name: '%{name[1]}' value: '%{value[1]}'" }
    } 
  if [value][0] {
    file {
      path => "\\host123\input\file.txt"		
      codec => line { format => "name: '%{name[0]}' value: '%{value[0]}'" }
    } 
}
```

---

<div class="post-metadata">

**Author:** ![drnjst](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@drnjst](https://discuss.elastic.co/u/drnjst)\
**Post date:** [October 12, 2018, 9:43am UTC](https://discuss.elastic.co/t/loop-over-array-in-output/151997/2 "2018-10-12T09:43:36Z")

</div>

I found a solution, for my case.  
I need to export the data to a other program, but this can handle arrays. So I split it up in there.

---

<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 9, 2018, 9:54am UTC](https://discuss.elastic.co/t/loop-over-array-in-output/151997/3 "2018-11-09T09:54:31Z")

</div>

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