2011-05-06 131 views
2

我有一个非常大的.txt文件,我想写一个ruby脚本来过滤一些数据。基本上我想遍历每一行,然后将单个单词存储在一个数组中的行中,然后对这些单词进行操作。然而我无法将每个单词分开排列在一个阵列中遍历数组中的每个字

tracker_file.each_line do|line| 
arr = "#{line}" 

我可以得到像这样的整行,但单个单词怎么样?

谢谢

回答

3

对字符串使用split方法。

irb(main):001:0> line = "one two three" 
=> "one two three" 
irb(main):002:0> line.split 
=> ["one", "two", "three"] 

所以,你的例子是:

tracker_file.each_line do |line| 
    arr = line.split 
    # ... do stuff with arr 
end 
0

你可以这样做:

tracker_file.each_line do |line| 
    arr = line.split 
# Then perform operations on the array 
end 

split方法将带分隔符的一个字符串分割成一个数组,在这种情况下,空间。

0

如果你正在阅读的东西写在英语和文本可能包含连字符,分号,空格,句号等,你可能会考虑一个正则表达式,如下列:

/[a-zA-Z]+(\-[a-zA-Z]+)*/ 

提取改为单词。

0

您不必使用IO#each_line,你也可以使用IO#each(separator_string)

另一种选择是使用IO#gets

while word = tracker_file.gets(/separator_regexp/) 
    # use the word 
end 
3
tracker_file.each_line do |line| 
    line.scan(/[\w']+/) do |word| 
    ... 
    end 
end 

如果没有需要遍历行,你可以直接迭代单词:

tracker_file.read.scan(/[\w']+/) do |word| 
    ... 
end