当数组中存在空条目时,如何添加值?

发布于 2024-10-08 22:45:54 字数 526 浏览 0 评论 0原文

我想创建一个实时序列数组。目前,我正在使用统计 gem 提取每个“天”的值:

define_statistic :sent_count, :count
=> :all, :group => 'DATE(date_sent)',    
:filter_on => {:email_id => 'email_id
> = ?'}, :order => 'DATE(date_sent) ASC'

它的作用是创建一个数组,其中有日期的值,例如

[["12-20-2010",1], ["12-24-2010",3]]

但我需要它来填充空值,所以它看起来更例如:

[["12-20-2010",1], ["12-21-2010",0], ["12-22-2010",0], ["12-23-2010",0], ["12-24-2010",3]]

请注意第二个示例如何为第一个数组中缺少的天数提供“0”值。

I want to create a real time-series array. Currently, I am using the statistics gem to pull out values for each 'day':

define_statistic :sent_count, :count
=> :all, :group => 'DATE(date_sent)',    
:filter_on => {:email_id => 'email_id
> = ?'}, :order => 'DATE(date_sent) ASC'

What this does is create an array where there are values for a date, for example

[["12-20-2010",1], ["12-24-2010",3]]

But I need it to fill in the null values, so it looks more like:

[["12-20-2010",1], ["12-21-2010",0], ["12-22-2010",0], ["12-23-2010",0], ["12-24-2010",3]]

Notice how the second example has "0" values for the days that were missing from the first array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一影成城 2024-10-15 22:45:54
#!/usr/bin/ruby1.8

require 'date'
require 'pp'

def add_missing_dates(series)
  series.map do |date, value|
    [Date.strptime(date, '%m-%d-%Y'), value]
  end.inject([]) do |series, date_and_value|
    filler = if series.empty?
               []
             else
               ((series.last[0]+ 1)..(date_and_value[0] - 1)).map do |date|
                 [date, 0]
               end
             end
    series + filler + [date_and_value]
  end.map do |date, value|
    [date.to_s, value]
  end
end

a = [["12-20-2010",1], ["12-24-2010",3]]
pp add_missing_dates(a)
# => [["2010-12-20", 1],
# =>  ["2010-12-21", 0],
# =>  ["2010-12-22", 0],
# =>  ["2010-12-23", 0],
# =>  ["2010-12-24", 3]]

我建议不要对基类进行猴子修补以包含此方法:它并不是通用的;它是一种通用的方法。即使有,它也不需要在那里。我会将它放在一个模块中,您可以将其混合到任何需要它的代码中:

module AddMissingDates
  def add_missing_dates(series)
    ...
  end
end

class MyClass
  include AddMissingDates
  ...
end

但是,如果您真的想要:

def Array.add_missing_dates(series)
  ...
end
#!/usr/bin/ruby1.8

require 'date'
require 'pp'

def add_missing_dates(series)
  series.map do |date, value|
    [Date.strptime(date, '%m-%d-%Y'), value]
  end.inject([]) do |series, date_and_value|
    filler = if series.empty?
               []
             else
               ((series.last[0]+ 1)..(date_and_value[0] - 1)).map do |date|
                 [date, 0]
               end
             end
    series + filler + [date_and_value]
  end.map do |date, value|
    [date.to_s, value]
  end
end

a = [["12-20-2010",1], ["12-24-2010",3]]
pp add_missing_dates(a)
# => [["2010-12-20", 1],
# =>  ["2010-12-21", 0],
# =>  ["2010-12-22", 0],
# =>  ["2010-12-23", 0],
# =>  ["2010-12-24", 3]]

I would recommend against monkey-patching the base classes to include this method: It's not all that general purpose; even if it were, it just doesn't need to be there. I'd stick it in a module that you can mix in to whatever code needs it:

module AddMissingDates
  def add_missing_dates(series)
    ...
  end
end

class MyClass
  include AddMissingDates
  ...
end

However, if you really want to:

def Array.add_missing_dates(series)
  ...
end
网白 2024-10-15 22:45:54

这是有效的:

#!/usr/bin/env ruby

require 'pp'
require 'date'

# convert the MM-DD-YYYY format date string to a Date
DATE_FORMAT = '%m-%d-%Y'
def parse_date(s)
  Date.strptime(s, DATE_FORMAT)
end

dates = [["12-20-2010",1], ["12-24-2010",3]]

# build a hash of the known dates so we can skip the ones that already exist.
date_hash = Hash[*dates.map{ |i| [parse_date(i[0]), i[-1]] }.flatten]

start_date_range = parse_date(dates[0].first) 
end_date_range   = parse_date(dates[-1].first)

# loop over the date range...
start_date_range.upto(end_date_range) do |d|
  # ...and adding entries for the missing ones.
  date_hash[d] = 0 if (!date_hash.has_key?(d))
end

# convert the hash back into an array with all dates
all_dates = date_hash.keys.sort.map{ |d| [d.strftime(DATE_FORMAT), date_hash[d] ] }
pp all_dates

# >> [["12-20-2010", 1],
# >>  ["12-21-2010", 0],
# >>  ["12-22-2010", 0],
# >>  ["12-23-2010", 0],
# >>  ["12-24-2010", 3]]

大多数代码都在准备事情,要么构建一个新数组,要么将日期对象返回到字符串。

This works:

#!/usr/bin/env ruby

require 'pp'
require 'date'

# convert the MM-DD-YYYY format date string to a Date
DATE_FORMAT = '%m-%d-%Y'
def parse_date(s)
  Date.strptime(s, DATE_FORMAT)
end

dates = [["12-20-2010",1], ["12-24-2010",3]]

# build a hash of the known dates so we can skip the ones that already exist.
date_hash = Hash[*dates.map{ |i| [parse_date(i[0]), i[-1]] }.flatten]

start_date_range = parse_date(dates[0].first) 
end_date_range   = parse_date(dates[-1].first)

# loop over the date range...
start_date_range.upto(end_date_range) do |d|
  # ...and adding entries for the missing ones.
  date_hash[d] = 0 if (!date_hash.has_key?(d))
end

# convert the hash back into an array with all dates
all_dates = date_hash.keys.sort.map{ |d| [d.strftime(DATE_FORMAT), date_hash[d] ] }
pp all_dates

# >> [["12-20-2010", 1],
# >>  ["12-21-2010", 0],
# >>  ["12-22-2010", 0],
# >>  ["12-23-2010", 0],
# >>  ["12-24-2010", 3]]

Most of the code is preparing things, either to build a new array, or return the date objects back to strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文