使用grep编写数组 [英] Writing Arrays using grep

查看:88
本文介绍了使用grep编写数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试搜索指定的字符串并将结果分配给数组。

打开并写入 input.txt和 ms3.txt文件可以正常工作。将普通字符串放在 reassign<<当我使用 line.grep 和它后面的正则表达式时, hello 的工作正常,它不会在控制台或ms3文件上打印任何内容甚至不会抛出任何错误

我也尝试过搜索并替换: reassign<< line.gsub(/ [abc] /,'£')

这是代码

I'm trying to search through a specified string and assign the results to an array.
Opening and writing to "input.txt" and "ms3.txt" files works fine. putting a normal string like reassign << "hello" in works fine its just when i use line.grep and the regex following it prints nothing to the console or the ms3 file it doesn't even throw up any errors
i've also tried a search and replace: reassign << line.gsub(/[abc]/, '£')
Here's the code

# encoding: utf-8
#!/usr/bin/ruby

file = File.open("input.txt", "w+")

reassign = []
  file.each_line do |line|
    reassign << line.grep(/[abc]/)
     end

new_file = File.open("ms3.txt", "w+")
new_file.puts(reassign)
new_file.close


推荐答案

您的代码可以简化使其变得更像Ruby,并使其表现更好:

Your code can be streamlined a lot to make it more Ruby-like, and to make it behave better:

# encoding: utf-8
#!/usr/bin/ruby

file = File.open("input.txt", "w+")

reassign = []
  file.each_line do |line|
    reassign << line.grep(/[abc]/)
    end

new_file = File.open("ms3.txt", "w+")
new_file.puts(reassign)
new_file.close




  1. #! 行必须是,因此请反转 encoding 和斜杠行。

  2. open 带一个块,这将使Ruby在该块退出时自动关闭文件。这是一项非常强大且明智的事情,因为它可以使您的文件I / O环境保持干净。可能会打开文件,但永远不会关闭文件,这通常是代码中的问题,如果循环执行,则可能耗尽计算机上的所有文件句柄,从而导致所有应用程序失败。使用阻止形式将避免这种情况。

  3. IO类具有 foreach ,这使得迭代文件行变得很简单。利用它而不是打开文件,然后使用 each_line 来利用它,因为它可以简化您的代码。

  1. #! lines have to be first, so reverse the encoding and "slash-bang" lines.
  2. open takes a block, which will allow Ruby to automatically close the file when the block exits. This is a very powerful, and smart thing to do, as it keeps your file I/O environment clean. It's possible, and often a problem as in your code, to open files but never close them, which can exhaust all the file handles on a machine if it's done in a loop, causing all apps to fail. Using the block form will avoid this.
  3. The IO class has foreach, which makes it simple to iterate over the lines of a file. Take advantage of it instead of opening the file then using each_line, because it simplifies your code.

这是我最初编写代码的方式:

Here's how I'd initially write your code:

#!/usr/bin/ruby
# encoding: utf-8

reassign = []
File.foreach("input.txt") do |line|
  reassign << line[/[abc]/]
end

File.write("ms3.txt", reassign.join("\n"))

但是,在重构之后,我最终得到:

But, after refactoring it I'd end up with:

#!/usr/bin/ruby
# encoding: utf-8

File.open('ms3.txt', 'w') do |fo|
  fo.puts File.foreach('input.txt').grep(/[abc/])
end




  1. 打开使用一个块打开输出文件,以利用在块时自动关闭文件的优势

  2. foreach 是一个迭代器,通常与一个块一起使用,以将读取的每一行传递到该块中。相反,我让 grep 读取找到的所有行并搜索模式。

  3. grep 作为数组返回到 puts ,它将遍历它们,并附加 \n 到每个结尾。

  4. fo.puts puts 的输出定向到输出文件。

  5. end 导致块退出,这导致 open 关闭文件。

  1. The open opens the output file using a block to take advantage of automatically closing the file when the block exits.
  2. foreach is an iterator, and normally is used with a block to pass each line read into the block. Instead, I'm letting grep read all the lines found and search for the pattern.
  3. Any lines found by grep that match the pattern are returned as an array to puts which will iterate over them, appending "\n" to the end of each.
  4. fo.puts directs the output of puts to the output file.
  5. end causes the block to exit, which causes open to close the file.

未经测试,但看起来正确。

That's untested but looks correct.

这篇关于使用grep编写数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆