On Wed, Dec 26, 2012 at 4:16 AM, Soichi Ishida <[email protected]> wrote:
> 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.
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
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en