# Access array in ruby loop

**URL:** https://discuss.elastic.co/t/access-array-in-ruby-loop/178028
**Category:** Logstash
**Created:** [April 23, 2019, 11:33am UTC](https://discuss.elastic.co/t/access-array-in-ruby-loop/178028 "2019-04-23T11:33:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![manunc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manunc/32/48340_2.png) [@manunc](https://discuss.elastic.co/u/manunc)
#### Post date: [April 23, 2019, 11:33am UTC](https://discuss.elastic.co/t/access-array-in-ruby-loop/178028/1 "2019-04-23T11:33:54Z")

</div>

Hello,

I would like to create new fields based on an array coming from a splitted field and associate the value of each fields

```
          dtab = data.split(/(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})(\w{20})\w{2}\w{2}\w{2}/)
      dtab.shift
      event.set('[metadata][dtab]', dtab)

      event.get('[metadata][dtab]').each_with_index do |hash, index|
        event.set( '[tire][' +[index]+ ']', hash[value])
      end

```

Source:  
"metadata": {  
"dtab": [  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff",  
"ff000000ffff807f00ff"  
]  
}

Result:  
[tire][1] = value1 (ff000000ffff807f00ff)  
[tire][2] = value2 (ff000000ffff807f00ff)  
...  
[tire][16] = value16 (ff000000ffff807f00ff)

BR

---

<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: [April 23, 2019, 12:18pm UTC](https://discuss.elastic.co/t/access-array-in-ruby-loop/178028/2 "2019-04-23T12:18:57Z")

</div>

What is the question?

---

<div class="post-metadata">

### Author: ![manunc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manunc/32/48340_2.png) [@manunc](https://discuss.elastic.co/u/manunc)
#### Post date: [April 23, 2019, 7:39pm UTC](https://discuss.elastic.co/t/access-array-in-ruby-loop/178028/3 "2019-04-23T19:39:27Z")

</div>

Sorry,

Fromthe [metadata][dtab] field I need to create fields:  
[tire][1] = value1 (ff000000ffff807f00ff)  
[tire][2] = value2 (ff000000ffff807f00ff)  
...  
[tire][16] = value16 (ff000000ffff807f00ff)

by using a ruby filter with a loop but I dont succeed to get index and value.  
Maybe I have to use only .each as there is no index in this case and create index by myself.

---

<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: [April 23, 2019, 8:36pm UTC](https://discuss.elastic.co/t/access-array-in-ruby-loop/178028/4 "2019-04-23T20:36:19Z")

</div>

Still not entirely clear. If you want

```
      "tire" => {
    "11" => "ff000000ffff807f00ff",
    "12" => "ff000000ffff807f00ff",
    "13" => "ff000000ffff807f00ff",
    "14" => "ff000000ffff807f00ff",
    "15" => "ff000000ffff807f00ff",
     "0" => "ff000000ffff807f00ff",
     "1" => "ff000000ffff807f00ff",
     "2" => "ff000000ffff807f00ff",
     "3" => "ff000000ffff807f00ff",
     "4" => "ff000000ffff807f00ff",
     "5" => "ff000000ffff807f00ff",
     "6" => "ff000000ffff807f00ff",
     "7" => "ff000000ffff807f00ff",
     "8" => "ff000000ffff807f00ff",
     "9" => "ff000000ffff807f00ff",
    "10" => "ff000000ffff807f00ff"
}

```

then you can use

```
        code => '
            event.get("[metadata][dtab]").each_with_index do |hash, index|
                event.set("[tire][#{index}]", hash)
            end
        '

```

String interpolation only works inside double quotes, which is why I almost always use single quotes to surround the code.

---

<div class="post-metadata">

### Author: ![manunc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manunc/32/48340_2.png) [@manunc](https://discuss.elastic.co/u/manunc)
#### Post date: [April 23, 2019, 8:55pm UTC](https://discuss.elastic.co/t/access-array-in-ruby-loop/178028/5 "2019-04-23T20:55:02Z")

</div>

That is what I need I guess.  
I was surrounded the code using double quote and wondering how to use double quote inside in order to use #{variable}. I will try.

---

<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: [May 21, 2019, 8:55pm UTC](https://discuss.elastic.co/t/access-array-in-ruby-loop/178028/6 "2019-05-21T20:55:08Z")

</div>

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