Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中同时读取两个文件_Ruby_File - Fatal编程技术网

在Ruby中同时读取两个文件

在Ruby中同时读取两个文件,ruby,file,Ruby,File,我有一个输入文本文件“input.txt”,看起来像这样 Country Code ID QTY FR B000X2D 75 130 FR B000X2E 75 150 我从每一行提取数据,因为我需要用提取的参数调用另一个Ruby脚本 class Enumerator def enum_drop(n) with_index do |val, idx| next if n == idx yield val end end end # f = F

我有一个输入文本文件“input.txt”,看起来像这样

Country Code ID QTY
FR  B000X2D 75 130
FR  B000X2E 75 150
我从每一行提取数据,因为我需要用提取的参数调用另一个Ruby脚本

class Enumerator
  def enum_drop(n)
    with_index do |val, idx|
      next if n == idx
      yield val
    end
  end
end
#
f = File.open("input.txt", "r")
f.each_line.enum_drop(0) do |line|
   country, code = line.chomp.split(/\s+/)
   check = getInfo.new
   info = check.Info(country, code) 
   DO AS DESCRIBED BELOW
end
info脚本的每一行都有以下输出“output.txt”:

FR  B000X2D ABC DEF GDI JKL 
我必须找到一个字符串“DEF”,如果给定代码中存在该字符串,我会将其附加到“input.txt”中的一行,以便在结尾处显示如下:

Country Code ID QTY TO_GO
    FR  B000X2D 75 130 DEF
如果它不存在,我将完全删除这一行


此外,我不确定在“input.txt”的末尾添加to_GO信息是否更容易,或者我是否应该创建一个新的文本文件,在其中合并“input.txt”和“output.txt”中的信息?

您在标题中提到的第二个文件在哪里?您的代码似乎只从一个文件中读取。您可以将前11行替换为:
file.foreach('input.txt')。drop(1)。每个do|line