Loop through fields to find a matching value

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???

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
        '
    }
1 Like

So beautiful, Thanks!

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

[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)...

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.

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