# How to loop through array in Logstash?

**URL:** <https://discuss.elastic.co/t/how-to-loop-through-array-in-logstash/213852>\
**Category:** Logstash\
**Created:** [January 6, 2020, 8:41am UTC](https://discuss.elastic.co/t/how-to-loop-through-array-in-logstash/213852 "2020-01-06T08:41:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmet\_Kartal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmet_kartal/32/47196_2.png) [@Ahmet\_Kartal](https://discuss.elastic.co/u/Ahmet_Kartal)\
**Post date:** [January 6, 2020, 8:41am UTC](https://discuss.elastic.co/t/how-to-loop-through-array-in-logstash/213852/1 "2020-01-06T08:41:42Z")

</div>

Hello,

I have this data:

```
"url" => [
      [0] [
          [0] "",
           [1] "data",
            [2] "data1"
[3] "9462"
        ],

```

What I want is that I need to get last element of list as assignment\_1 ( I need to assing last data into this variable) and the other things need to placed in assignment\_2 as "data/data1".  
I though it would be useful if have used loop over array list but could not figured it out.  
Think that my array lenght will be changed all the time.But again, I need last data into assignmet\_1 and the other into assingment\_2.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [January 6, 2020, 5:59pm UTC](https://discuss.elastic.co/t/how-to-loop-through-array-in-logstash/213852/2 "2020-01-06T17:59:34Z")

</div>

I would do that in ruby. Start off with

```
ruby {
    code => '
        url = event.get("url")
        if url
            event.set("a1", url[0][-1])
            event.set("a2", url[0][0..-2].join("/"))
        end
    '
}
```

---

<div class="post-metadata">

**Author:** ![Ahmet\_Kartal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmet_kartal/32/47196_2.png) [@Ahmet\_Kartal](https://discuss.elastic.co/u/Ahmet_Kartal)\
**Post date:** [January 7, 2020, 6:28am UTC](https://discuss.elastic.co/t/how-to-loop-through-array-in-logstash/213852/3 "2020-01-07T06:28:31Z")

</div>

Thank you so much. It works really good. Especially, I was suffering to code

> [@Badger](#):
>
> event.set("a2", url[0][0..-2].join("/"))

this part.  
This might be a another question but when I split a field logstash deletes its value instead of keeping it. I want to split a field but dont want to loose its value. How am I gonna do that?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [January 7, 2020, 2:52pm UTC](https://discuss.elastic.co/t/how-to-loop-through-array-in-logstash/213852/4 "2020-01-07T14:52:40Z")

</div>

You need to show an example of the event input, the filter configuration, and the resulting event.

---

<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:** [February 4, 2020, 2:53pm UTC](https://discuss.elastic.co/t/how-to-loop-through-array-in-logstash/213852/5 "2020-02-04T14:53:10Z")

</div>

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