[英]How to access multiple lines at once in ruby
我正在编写一个程序来解析基本文本文件,并将其中的某些行与测试结果进行比较。 我使用特定的单词来查找应该与测试结果进行比较的行,然后根据行是否与结果匹配(它们应该完全相同)来通过或失败结果。 我正在使用以下常规格式:
File.open(file).each do |line|
if line include? "Revision"
if line==result
puts "Correct"
else
puts "Fail"
大多数情况只是一行,所以很容易。 但是在某些情况下,我的结果是4行,而不仅仅是1行。 因此,一旦找到所需的行,就需要检查结果是否等于感兴趣的行加上其后的以下3行。 这是在读取的文件中格式化信息的方式,以及测试结果的外观:
Product Serial Number: 12058-2865
Product Part Number: 3456
Product Type: H-Type
Product Version: 2.07
找到兴趣线后,我只需要比较兴趣线和接下来的三行与整个结果即可。
if line include? "Product Serial Number"
#if (#this line and the next 3) == result
puts Correct
else
puts "Fail"
我该怎么做呢?
text =<<_
My, oh my
Product Serial Number: 12058-2865
Product Part Number: 3456
Product Type: H-Type
Product Version: 2.07
My, oh my
Product Serial Number: 12058-2865
Product Part Number: 3456
Product Type: H-Type
Product Version: 2.08
My, ho my
Product Serial Number: 12058-2865
Product Part Number: 3456
Product Type: H-Type
Product Version: 2.07
_
result =<<_.lines
Product Serial Number: 12058-2865
Product Part Number: 3456
Product Type: H-Type
Product Version: 2.07
_
#=> ["Product Serial Number: 12058-2865\n", "Product Part Number: 3456\n",
# "Product Type: H-Type\n", "Product Version: 2.07\n"]
FName = "test"
File.write(FName, text)
#=> 339
target = "Product Serial Number"
nbr_result_lines = result.size
#=> 4
lines = File.readlines(FName)
#=> ["My, oh my\n",
# "Product Serial Number: 12058-2865\n",
# ...
# "Product Version: 2.07\n"]
lines.each_with_index do |line, i|
(puts (lines[i, nbr_result_lines] == result ? "Correct" : "Fail")) if
line.match?(target)
end
# "Correct"
# "Fail"
# "Correct"
请注意,当i
足够大时lines[i, nbr_result_lines]
数组lines[i, nbr_result_lines]
将以一个或多个nil
结束。
如果文件太大,以至于无法将其混入一个数组中,则可以
nbr_result_lines
读入缓冲区(使用IO :: foreach ); target
与缓冲区的第一行进行比较,如果匹配,则将result
与缓冲区进行比较; 好吧,您可以使用几种方法来完成此操作,最简单的方法是遍历每一行。 并尝试像这样检测序列,它应该类似于用于检测序列的状态机:
step = 0
File.open('sample-file.txt').each do |line|
if /^Product Serial Number.*/.match? line
puts(step = 1)
elsif /^Product Part Number.*/.match?(line) && step == 1
puts(step = 2)
elsif /^Product Type.*/.match?(line) && step == 2
puts(step = 3)
elsif /^Product Version.*/.match?(line) && step == 3
puts 'correct'
puts(step = 0)
else
puts(step = 0)
end
end
结果如下:
ruby read_file.rb
1
2
3
correct
0
0
1
0
0
0
0
0
0
1
2
3
correct
0
0
和此示例文件:
Product Serial Number: 12058-2865
Product Part Number: 3456
Product Type: H-Type
Product Version: 2.07
no good line
Product Serial Number: 12058-2865
BAD Part Number: 3456
Product Type: H-Type
Product Version: 2.07
no good line
no good line
no good line
Product Serial Number: 12058-2865
Product Part Number: 3456
Product Type: H-Type
Product Version: 2.07
no good line
存在类似的回答问题: 一次读取多行
我认为,如果您拥有格式已知的文件并保留了一系列行,则可以读取乘以行的数组,并使用所需的逻辑遍历数组元素。
File.foreach("large_file").each_slice(8) do |eight_lines| # eight_lines is an array containing 8 lines. # at this point you can iterate over these lines end
是的循环不是很好,但最好是乘以if else
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.