I'm trying to sum the value of a specific field every time it shows, the field is in this format: [cdr][Service-Information][PS-Information][Service-Data-Container][Accounting-Output-Octets]
and its value is a numeric field (it shows the number of bits consumed).
What I'm trying to do is the following:
a = event.get("[cdr][Service-Information][PS-Information][Service-Data-Container][Accounting-Output-Octets]")
if a
sum = 0
a.each_index { |x|
sum += a["amount"]
}
event.set("amount-sum", sum)
end
I'm getting the following error:
Ruby exception occurred: undefined method `each_index' for Integer
I am a newbie in Ruby, so I've got no idea if this code serves for this type of field too.
If a is integer You can use something like this:
a = 123456789
a = a.to_s.split("").map(&:to_i)
sum = 0
a.each do |x|
sum += x
end
p sum #=> 45
And please DO NOT USE brace if block is not in one line. Here I did it with do end just to show how it must be. If You want to use one line block - You must write like that:
a.each {|x| sum += x}
if block > 1 lines than use do/end | else { }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.