Accumulate total count for each month

I have a file that contains a list of STRING datetime like

Thu Dec 20 00:58:17 +0000 2012
Thu Dec 20 00:50:18 +0000 2012
Thu Dec 20 00:48:53 +0000 2012
Thu Dec 20 00:44:43 +0000 2012
Thu Dec 20 00:42:32 +0000 2012

Each line represents the occurrence of one event.
I want to count the number of events occurred each month (or day).

First, I have converted to DateTime objects. It’s easy.

Normally, when counting the number of each word occurred in a file,
using hash can help (each word as a key). But this case, it is not that
simple because the key is date object.

Do you have better ideas for counting the number of events each month?

soichi

Soichi I. wrote in post #1090212:

Normally, when counting the number of each word occurred in a file,
using hash can help (each word as a key). But this case, it is not that
simple because the key is date object.

Do you have better ideas for counting the number of events each month?

require ‘date’

date_times1 = (1…12).map do |month|
DateTime.new(2012,month,25,4,5,6)
end

date_times2 = (1…6).map do |month|
DateTime.new(2012,month,15,4,5,6)
end

date_times = date_times1 + date_times2

results = Hash.new(0)

date_times.each do |dt|
results[dt.month] += 1
end

(1…12).each do |month|
puts “month %s: %s” % [month, results[month]]
end

–output:–
month 1: 2
month 2: 2
month 3: 2
month 4: 2
month 5: 2
month 6: 2
month 7: 1
month 8: 1
month 9: 1
month 10: 1
month 11: 1
month 12: 1

Subject: Accumulate total count for each month
Date: Wed 26 Dec 12 12:16:26PM +0900

Quoting Soichi I. ([email protected]):

Normally, when counting the number of each word occurred in a file,
using hash can help (each word as a key). But this case, it is not that
simple because the key is date object.

Do you have better ideas for counting the number of events each
month?

Why not use the year+month as the hash key? From your DateTime object,
you obtain an appropriate string with dt.strftime(’%Y%m’).

Carlo

(1…12).each do |month|
puts “%s: %s” % [Date::MONTHNAMES[month], results[month]]
end

–output:–
January: 2
February: 2
March: 2
April: 2
May: 2
June: 2
July: 1
August: 1
September: 1
October: 1
November: 1
December: 1

Wow, thanks everyone.
There are a lot of ideas I could not figure out myself!

soichi

If that is all there is to it, a simple split would work

nevents = Hash.new(0)

ARGF.each_line do |line|
month, year = line.chomp.split.values_at(1, -1)
nevents["#{year} #{month}"] += 1
end

On Wed, Dec 26, 2012 at 4:16 AM, Soichi I. [email protected]
wrote:

I want to count the number of events occurred each month (or day).

First, I have converted to DateTime objects. It’s easy.

Better use Date since you want to count occurrences at dates.

Normally, when counting the number of each word occurred in a file,
using hash can help (each word as a key). But this case, it is not that
simple because the key is date object.

Why isn’t that simple? Did you actually try it?

Do you have better ideas for counting the number of events each month?

require ‘date’

counts = Hash.new 0

ARGF.each do |line|
line.chomp!
counts[Date.parse(line)] += 1
end

counts.sort_by {|d,| k}.each do |d, c|
printf “%-20s %6d\n”, d, c
end

Cheers

robert

On Wed, Dec 26, 2012 at 10:59 PM, 7stud – [email protected] wrote:

line.chomp!

1.rb:10:in block in <main>': undefined local variable or methodk’ for
main:Object (NameError)

Typo when ad hoc refactoring names

require ‘date’

counts = Hash.new 0

ARGF.each do |line|
line.chomp!
counts[Date.parse(line)] += 1
end

counts.sort_by {|d,| d}.each do |d, c|
printf “%-20s %6d\n”, d, c
end

Cheers

robert

Robert K. wrote in post #1090246:

Why isn’t that simple? Did you actually try it?

require ‘date’

counts = Hash.new 0

ARGF.each do |line|
line.chomp!
counts[Date.parse(line)] += 1
end

counts.sort_by {|d,| k}.each do |d, c|
printf “%-20s %6d\n”, d, c
end

Cheers

robert

1.rb:10:in block in <main>': undefined local variable or methodk’ for
main:Object (NameError)

.
.
.
.
.
.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs