How to sum together values in different objects

Hi, I have nested objects in an array. The objects are the same structure. I want to add together all [array][amount] values (which are all numbers) where x can be any number up to around 40, to create a new field [amount-sum] where this arithmetic is stored.

There is not a set amount of objects in each array and so this method does not work:

event.set( "amount-sum", event.get( "[array][0][amount]" ) + event.get( "[array][1][amount]" ) + event.get( "[array][2][amount]" ) )

Thanks,

Tom

I haven't tested it, but you could do that in a ruby filter. Something like

ruby {
    code => '
        a = event.get("[array]")
        if a
            sum = 0
            a.each_index { |x|
                sum += a[x]["amount"]
            }
            event.set("amount-sum", sum)
        end
    '
}

You are the best person ever, thankyou

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