tags:

views:

34

answers:

1

I have a file with data like this:

01 JUL something
       something
       something              445
       something else
01 JUL whatever
       everwa3
       lklkj                  445
       something else
02 JUL ljkjlkj
       ljkljlkj 
       lkjkjlk                500
       lkjkj
02 JUL ljlkjklj
       lkjkjlkj
       lkjkj                  500
       lkjlkj

In the end I would like to find out how many occurences of 01 JUL 445 there are and 02 JUL 500 there are

in this case it would be..

01 JUL 445 = 2

02 JUL 500 = 2

I am able to read in lines and get the data out ...how can I go about counting the same things?

+1  A: 
counts = {}
date = ""
file.readlines.each_with_index do |l, i|
  if i % 4 == 0 # first line
    date = l.split("\t").first
  elsif i % 4 == 3 # third line
    wierd_num = l.split("\t").last
    counts[date+" "+wierd_num] ||= 0
    counts[date+" "+wierd_num] += 1
  end
end

puts counts # => {"01 JUL 445" => 2, "02 JUL 500" => 2}
Jakub Hampl
Thanks. Though, now I am having trouble with UTF-8 characters. please see http://stackoverflow.com/questions/2897398/broken-utf-8-string-ruby
josh