Ruby maths operation error

Hi..
ruby {

    code => "event['discount'] = [[event['mrp'].to_i - event['price'].to_i].to_f / event['mrp'].to_f ] * 100"           

}
But the above code some what wrong....can anybody explain where I am wrong...

Thanks

What version of Logstash? If 5 or later see https://www.elastic.co/guide/en/logstash/current/event-api.html for how to get and set field values.

Also, you're using square brackets when you should be using parentheses.

((event['mrp'].to_i - event['price'].to_i).to_f / event['mrp'].to_f ) * 100

Sir,

I edit the code as per latest logstash rule...which is given below. But still some error which are also given below:
code =>
'event.set(
"discount",(((event.get["mrp"].to_f - event.get["price"].to_f) / event.get["mrp"].to_f ) * 100)
)'

ERROR] 2017-11-24 07:12:51.030 [[main]>worker7] ruby - Ruby exception occurred: wrong number of arguments calling get (0 for 1)
[ERROR] 2017-11-24 07:12:51.030 [[main]>worker7] ruby - Ruby exception occurred: wrong number of arguments calling get (0 for 1)
[ERROR] 2017-11-24 07:12:51.031 [[main]>worker7] ruby - Ruby exception occurred: wrong number of arguments calling get (0 for 1)
[ERROR] 2017-11-24 07:12:51.031 [[main]>worker7] ruby - Ruby exception occurred: wrong number of arguments calling get (0 for 1)
[ERROR] 2017-11-24 07:12:51.031 [[main]>worker7] ruby - Ruby exception occurred: wrong number of arguments calling get (0 for 1)
If any one know kindly let me know where I am wrong.
Thanks

Read the documentation more carefully. There should be no square brackets after event.get.

Thanks sir...I got result. Here I am uploading my code....which is final solution. So if any body need they can use it...
filter{
ruby{
code =>
'event.set(
"discount",(((event.get("mrp").to_f - event.get("price").to_f) / event.get("mrp").to_f ) * 100)
)'
}
}
Thanks.

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