如何解析文本文件并计算字符串的出现?

时间:2014-04-08 08:00:01

标签: ruby ruby-1.9.3

我的文字文件包含日期列表

2014-01-18
2014-01-18
2014-01-20
2014-01-20
2014-01-20
2014-01-21
2014-01-21
2014-01-22
2014-01-22
2014-01-22
2014-01-22
2014-01-22

如何计算每个日期的记录次数?所以输出类似于:

2014-01-18 2
2014-01-19 0
2014-01-20 3
2014-01-21 2
2014-01-22 5

3 个答案:

答案 0 :(得分:4)

path = '/path/to/file'
lines = File.readlines(path).map(&:chomp)

# At this point lines should look like below, this is just for testing
lines = ["2014-01-18", "2014-01-18", "2014-01-20", 
         "2014-01-20", "2014-01-20", "2014-01-21", 
         "2014-01-21", "2014-01-22", "2014-01-22", 
         "2014-01-22", "2014-01-22", "2014-01-22"]

# All Ruby versions (since you're using Ruby 1.9.3 you should use tihs)
Hash[ lines.group_by { |v| v }.map { |k, v| [k, v.size] } ]
# Ruby >= 2.1.0
lines.group_by { |v| v }.map { |k, v| [k, v.size] }.to_h

#=> {"2014-01-18"=>2, "2014-01-20"=>3, "2014-01-21"=>2, "2014-01-22"=>5}

答案 1 :(得分:3)

我喜欢使用Hash.new

lines = ["2014-01-18", "2014-01-18", "2014-01-20", 
         "2014-01-20", "2014-01-20", "2014-01-21", 
         "2014-01-21", "2014-01-22", "2014-01-22", 
         "2014-01-22", "2014-01-22", "2014-01-22"]

result = Hash.new(0)
lines.each { |line| result[line] += 1 }

result
# => {"2014-01-18"=>2, "2014-01-20"=>3, "2014-01-21"=>2, "2014-01-22"=>5}

答案 2 :(得分:2)

lines = File.readlines('file.txt').map(&:chomp)

op = Hash.new(0)

lines.each do |line|
  op[line.to_sym] += 1
end

puts op.sort_by { |k, v| v }