# Modifying an arrays value by index

**URL:** https://discuss.elastic.co/t/modifying-an-arrays-value-by-index/221045
**Category:** Logstash
**Created:** [February 26, 2020, 1:21pm UTC](https://discuss.elastic.co/t/modifying-an-arrays-value-by-index/221045 "2020-02-26T13:21:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Viktor\_Zsolnai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/viktor_zsolnai/32/63336_2.png) [@Viktor\_Zsolnai](https://discuss.elastic.co/u/Viktor_Zsolnai)
#### Post date: [February 26, 2020, 1:21pm UTC](https://discuss.elastic.co/t/modifying-an-arrays-value-by-index/221045/1 "2020-02-26T13:21:28Z")

</div>

Hello,  
I'm pretty new to logstash, and I need to replace a value in an array, using a kv filter's value,  
but when I want to use the index variable nothing happens, if I use a simple integer instead, it works fine, here is my code I'm using:  
"template" =\> [  
[0] "Ön lekérdezte ",  
[1] "id",  
[2] " azonosítójú kártyája adatait"  
],  
ruby {code =\> "

```
            event.get('template').each_with_index do |item,index|
              hash = event.get('kv')
              hash.each { |key,value|
                if key.include? 'id'
                    event.set('[template][#{index}]', value)
                  end
                }
             end

```

"}

Any ideas what I'm doing wrong?  
Thank you

---

<div class="post-metadata">

### Author: ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)
#### Post date: [February 27, 2020, 4:40pm UTC](https://discuss.elastic.co/t/modifying-an-arrays-value-by-index/221045/2 "2020-02-27T16:40:56Z")

</div>

Hi there,

you using interpolation with single quoted string. You need double quotes to use it.

---

<div class="post-metadata">

### Author: ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)
#### Post date: [February 27, 2020, 4:57pm UTC](https://discuss.elastic.co/t/modifying-an-arrays-value-by-index/221045/3 "2020-02-27T16:57:10Z")

</div>

I'd refactor your code as following

```
filter {
  ruby {
    code => '
      hash = event.get("kv")
      event.get("template").each_with_index do |item, index|
        hash.each { |k, v| event.set("[template][#{index}]", v) if k.include? "id" }
      end
    '
  }
}
```

---

<div class="post-metadata">

### Author: ![Viktor\_Zsolnai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/viktor_zsolnai/32/63336_2.png) [@Viktor\_Zsolnai](https://discuss.elastic.co/u/Viktor_Zsolnai)
#### Post date: [March 6, 2020, 1:24pm UTC](https://discuss.elastic.co/t/modifying-an-arrays-value-by-index/221045/4 "2020-03-06T13:24:34Z")

</div>

Thank you! 🙂

---

<div class="post-metadata">

### Author: ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)
#### Post date: [March 6, 2020, 1:39pm UTC](https://discuss.elastic.co/t/modifying-an-arrays-value-by-index/221045/5 "2020-03-06T13:39:12Z")

</div>

No problem

---

<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: [April 3, 2020, 1:39pm UTC](https://discuss.elastic.co/t/modifying-an-arrays-value-by-index/221045/6 "2020-04-03T13:39:14Z")

</div>

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