如何使用RUBY比较CSV中的交替行 [英] How to compare alternating rows in CSV using RUBY

查看:96
本文介绍了如何使用RUBY比较CSV中的交替行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数千行的数据集.我想计算一下警报在ALARM_OPENED和ALARM_NORMALIZED之间切换的次数

I have a data set that consists thousand of rows. I would like to count how many times an alarm toggle between ALARM_OPENED and ALARM_NORMALIZED

这是数据样本.警报两次触发,因此理想情况下是count = 2

Here is a data sample. The Alarm toggle twice and hence ideally the count = 2

现在的问题是我不知道怎么做

The issue now is I cannot figure how to

1)比较事件类型的ALARM _OPENED和ALARM_NORMALIZED

1) compare ALARM _OPENED and ALARM_NORMALIZED for the event type

2)比较事件更改之间的时间差(切换应在两秒的时间范围内进行.)

2) To compare the difference in time between the change in event (the toggling should happen within a time frame of two seconds.)

count = 0 
#loop this 
if event_type[0] = 'ALARM_OPENED' 
  if event_type[1] = 'ALARM_NORMALIZED'
    #time[0] - time[1] = 2 seconds
    count = count + 1
  end
end

p count

推荐答案

如果可以假设您总是有一堆OPENED/NORMALIZED对,则可以将数组切成对:

If you can assume that you always have a bunch of OPENED/NORMALIZED pairs, you can slice the array into pairs:

event_type.each_slice(2) do |opened, normalized|
  break unless normalized # unpaired event at the end
  # whatever you want to do with the two events here
end

这篇关于如何使用RUBY比较CSV中的交替行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆