Using a variable as an index of an array of patterns

I have this filter in a logstash pipeline

filter{

    ruby { code => 'event.set("patternmatch1", event.get("message").scan(/TEXT.*?(\w[\w,.]*)\r?$/))'
    }
    # Retrieve the lenght of the array cointaining all the matches
    ruby { code => 'event.set("number_of_elements", event.get("patternmatch4").length-1)'
    }

My goal

I would like to use the array length as the index of a ruby array of patterns to choose index pattern that i want. Instead of using index 9:

mutate{
       add_field => {"gross_profit" => "%{[patternmatch4][9]}"}}
}

I want to reference the last pattern match of the array with the variable containing the lenght of the array "number_of_elements", but is not working. Maybe i am incorrectly referencing the variable. How to reference that variable correctly?

mutate{
   add_field => {"gross_profit" => "%{[patternmatch4][%{[number_of_elements]}"}}
}

Depending on where you want use it, [arrayName][-1] may work.

Thank you Badger,

[-1] is working perfectly

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