有5个文件file1.txt file2.txt....file5.txt
然后我有3个字red white blue
我如何收集特定类型的数据与Ruby脚本
我试图找出有多少次,在red white blue
发生哪些文件列表。
最后的格式应该是:
red = file1.txt, file3.txt, 2
white = file2.txt, 1
blue = file1.txt, file2.txt, file3.txt, 3
这是我到目前为止有:
files.each do |i|
curfile = File.new("#{i}","r")
while (line = curfile.gets)
mywords.each do |j|
if (line ~= /\b#{j}\b/)
##what kind of data structure should I put the results in??
end
end
end
end
我应该把什么样的数据结构的结果?
怎么样列出每个颜色的数组中的文件?所以'red = [“file1.txt”,“file3.txt”]'等等。然后,使用'red.length'输出它出现的次数。 – 2010-09-07 02:25:17
作业?使用散列,其中每个键是颜色,并且该键的相关值每增加1次颜色的次数就会加1。 – dawg 2010-09-07 02:28:04
@drewk:询问多维哈希是一个合理的问题。我认为还没有令人满意的答案。 – 2010-09-07 12:46:59