减去哈希值

时间:2018-02-25 03:26:30

标签: ruby-on-rails ruby

我想从两个哈希值中减去两个值。要知道我的增长增量。

我的哈希看起来像这样:

[
 {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16617331511, :timeCapturedmax=>"February 18, 2018 at 09:27 AM", :time_unixmax=>1518964024}, 
 {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16617216094, :timeCapturedmax=>"February 18, 2018 at 09:22 AM", :time_unixmax=>1518963724}, 
 {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16617202279, :timeCapturedmax=>"February 18, 2018 at 09:18 AM", :time_unixmax=>1518963485}, 
 {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16616985649, :timeCapturedmax=>"February 18, 2018 at 09:12 AM", :time_unixmax=>1518963135}, 
 {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16616404836, :timeCapturedmax=>"February 18, 2018 at 09:07 AM", :time_unixmax=>1518962829},  
 {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16616368250, :timeCapturedmax=>"February 18, 2018 at 09:03 AM", :time_unixmax=>1518962583}
]

我想要以下结果:

Rate_Discard = [{:device=>"router", :discard_sub=>115417}, {:device=>"router", :discard_sub=>13815}, {:device=>"router", :discard_sub=>216630}]

1 个答案:

答案 0 :(得分:0)

arr = [
  {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16617331511, 
     :timeCapturedmax=>"February 18, 2018 at 09:27 AM", :time_unixmax=>1518964024},
  {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16617216094,
     :timeCapturedmax=>"February 18, 2018 at 09:22 AM", :time_unixmax=>1518963724},
  {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16617202279,
     :timeCapturedmax=>"February 18, 2018 at 09:18 AM", :time_unixmax=>1518963485},
  {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16616985649,
     :timeCapturedmax=>"February 18, 2018 at 09:12 AM", :time_unixmax=>1518963135},
  {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16616404836,
     :timeCapturedmax=>"February 18, 2018 at 09:07 AM", :time_unixmax=>1518962829},
  {:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16616368250,
     :timeCapturedmax=>"February 18, 2018 at 09:03 AM", :time_unixmax=>1518962583}
]

arr.each_cons(2).map do |g,h|
  { device: g[:device], discard_sub: g[:discardmax]-h[:discardmax] }
end
  #=> [{:device=>"router", :discard_sub=>115417},
  #    {:device=>"router", :discard_sub=>13815},
  #    {:device=>"router", :discard_sub=>216630},
  #    {:device=>"router", :discard_sub=>580813},
  #    {:device=>"router", :discard_sub=>36586}]