我有两个文本文件,master.txt和926.txt.如果926.txt中有一行不在master.txt中,我想写一个新文件notinbook.txt.
我写了我能想到的最好的东西,但鉴于我是一个可怕的/新手程序员,它失败了.这就是我所拥有的
g = File.new("notinbook.txt", "w")
File.open("926.txt", "r") do |f|
while (line = f.gets)
x = line.chomp
if
File.open("master.txt","w") do |h|
end
while (line = h.gets)
if line.chomp != x
puts line
end
end
end
end
end
g.close
Run Code Online (Sandbox Code Playgroud)
当然,它失败了.谢谢!
如果我
在文本文件中有一个数字列表,如
112536
523534
241255
233345
212121
.
我想找到一个数字连续三次重复或两位数连续重复三次的数字,我该怎么做?
愚蠢的做法是这样的
while (line = f.gets)
g.puts line if line =~ /111/
g.puts line if line =~ /222/
g.puts line if line =~ /333/
etc...
Run Code Online (Sandbox Code Playgroud)
但这显然效率不高.有更简单的方法吗?