# Extract a integer from an array based in field values

**URL:** <https://discuss.elastic.co/t/extract-a-integer-from-an-array-based-in-field-values/227498>\
**Category:** Logstash\
**Created:** [April 10, 2020, 12:27pm UTC](https://discuss.elastic.co/t/extract-a-integer-from-an-array-based-in-field-values/227498 "2020-04-10T12:27:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krille.com](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@krille.com](https://discuss.elastic.co/u/krille.com)\
**Post date:** [April 10, 2020, 12:27pm UTC](https://discuss.elastic.co/t/extract-a-integer-from-an-array-based-in-field-values/227498/1 "2020-04-10T12:27:58Z")

</div>

Hello...

I am trying to extract a forex rate from an array based on the year and month fields. I have a array which is like this:

```auto
// forexRate =>
// 2017;7;0.0001105093, 2017;8;0.0001116904, 2017;9;0.0001114438, 2017;10;0.0001118130, 2017;11;0.0001110309, 2017;12;0.0001111167, 2018;1;0.0001111290, 2018;2;0.0001113834, 2018;3;0.0001113524;

```

I have already fields of integer for _year_ and _month_ which are:

```auto
// year => 2017
// month => 9

```

I want to user the _year_ and _month_ to pick the third value

```auto
// grok {
// match => ["forexRate" , "%[year];%[month];%{NUMBER:rate}" ]
// add_field => ["reportRate" , "%{rate}"]
// }

```

Then use the _rate_ to calculate a _newCurrency_

```auto
// ruby {
      code => 'event.set("newCurrency", event.get("numberCount").to_i * 1 * [rate])
         }

```

How can I use the fields _year_ and _month_ to pick the third value in the array??

---

<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 10, 2020, 1:57pm UTC](https://discuss.elastic.co/t/extract-a-integer-from-an-array-based-in-field-values/227498/2 "2020-04-10T13:57:12Z")

</div>

If you use

```
output { stdout { codec => rubydebug } }

```

what does the array look like?

---

<div class="post-metadata">

**Author:** ![krille.com](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@krille.com](https://discuss.elastic.co/u/krille.com)\
**Post date:** [April 10, 2020, 2:40pm UTC](https://discuss.elastic.co/t/extract-a-integer-from-an-array-based-in-field-values/227498/3 "2020-04-10T14:40:07Z")

</div>

Sorry, can't get that to work...

---

<div class="post-metadata">

**Author:** ![krille.com](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@krille.com](https://discuss.elastic.co/u/krille.com)\
**Post date:** [April 11, 2020, 11:45am UTC](https://discuss.elastic.co/t/extract-a-integer-from-an-array-based-in-field-values/227498/4 "2020-04-11T11:45:23Z")

</div>

I found an answer from you on a different thread:

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

```

No I have each entry from the array as separated fields...

Looking how to loop through all fields into grok match...

Cheers

---

<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 9, 2020, 11:45am UTC](https://discuss.elastic.co/t/extract-a-integer-from-an-array-based-in-field-values/227498/5 "2020-05-09T11:45:26Z")

</div>

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