# Loop through fields to find a matching value

**URL:** <https://discuss.elastic.co/t/loop-through-fields-to-find-a-matching-value/227601>\
**Category:** Logstash\
**Created:** [April 11, 2020, 12:12pm UTC](https://discuss.elastic.co/t/loop-through-fields-to-find-a-matching-value/227601 "2020-04-11T12:12:29Z")\
**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 11, 2020, 12:12pm UTC](https://discuss.elastic.co/t/loop-through-fields-to-find-a-matching-value/227601/1 "2020-04-11T12:12:29Z")

</div>

I have 2 fields (integer) called _Year_ and _Month_ e.g.  
Year = 2016  
Month = 9

I have from an array created seqRate.## fields e.g.  
"rateSeq": {  
"0": "﻿2016;8;0.0001285760",  
"1": "2016;9;0.0001287969",  
"2": "2016;10;0.0001302932",

I would like to match through the fields (number of fields created is dynamic) and add a field of the third value, to later be used as input for calculating a number...

How can I get the third value when first value and second value matches _Year_ and _Month_???

---

<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 11, 2020, 3:48pm UTC](https://discuss.elastic.co/t/loop-through-fields-to-find-a-matching-value/227601/2 "2020-04-11T15:48:00Z")

</div>

You could use

```
    ruby {
        code => '
            year = event.get("year")
            month = event.get("month")
            pattern = /^#{year};#{month};/
            rs = event.get("rateSeq")
            if rs.is_a? Hash
                rs.each { |k, v|
                    if v.match?(pattern)
                        event.set("someField", v.sub(pattern, "").to_f)
                    end
                }
            end
        '
    }
```

---

<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 12, 2020, 4:59am UTC](https://discuss.elastic.co/t/loop-through-fields-to-find-a-matching-value/227601/3 "2020-04-12T04:59:14Z")

</div>

So beautiful, Thanks!

Still something is not ok with the code, I get this message printed 21 times in the log:

```auto
[2020-04-12T06:53:07,691][ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method `match?' for "2019;9;0.0001093494":String

```

I have 60 field entries of seqRate.## (0-59)...

---

<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 12, 2020, 2:01pm UTC](https://discuss.elastic.co/t/loop-through-fields-to-find-a-matching-value/227601/4 "2020-04-12T14:01:31Z")

</div>

If you have 60 entries and you get 21 errors then that is saying that one third of the time the String class in your Ruby does not have a match? method. I cannot imagine what could cause that.

---

<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 11, 2020, 8:58am UTC](https://discuss.elastic.co/t/loop-through-fields-to-find-a-matching-value/227601/7 "2020-05-11T08:58:00Z")

</div>

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