Ruby 将匹配项从放入文件

Ruby 将匹配项从放入文件,ruby,Ruby,我在mac1.txt和mac2.txt中有mac地址,我想这样做: v = File.open("/RubyDev/sort/mac1.txt",'r').each_line do |a| w = File.open("/RubyDev/sort/mac2.txt",'r').each_line do |b| if w in v puts w end end end File.open("m

我在mac1.txt和mac2.txt中有mac地址,我想这样做:

v = File.open("/RubyDev/sort/mac1.txt",'r').each_line do |a|
      w = File.open("/RubyDev/sort/mac2.txt",'r').each_line do |b|
            if w in v
                puts w
            end
      end
end
File.open("mac1.txt",'r').each_line do |a|
  File.open("mac2.txt",'r').each_line do |b|
    if a == b
      puts b
    end
  end
end
common_macs = File.open("mac1.txt").readlines.map(&:chomp) & 
  File.open("mac2.txt").readlines.map(&:chomp)

提前感谢您的帮助

编辑:下面的第一个版本实际上非常糟糕。这里有一个更好的版本:

lines = []

File.open("mac1.txt",'r').each_line do |a|
  lines.push(a.rstrip!)
end

File.open("mac2.txt",'r').each_line do |b|
  if lines.include?(b.rstrip!)
    puts b
  end
end

我想你要找的是这样的东西:

v = File.open("/RubyDev/sort/mac1.txt",'r').each_line do |a|
      w = File.open("/RubyDev/sort/mac2.txt",'r').each_line do |b|
            if w in v
                puts w
            end
      end
end
File.open("mac1.txt",'r').each_line do |a|
  File.open("mac2.txt",'r').each_line do |b|
    if a == b
      puts b
    end
  end
end
common_macs = File.open("mac1.txt").readlines.map(&:chomp) & 
  File.open("mac2.txt").readlines.map(&:chomp)

对吗?如果没有,你能提供更多关于这个问题的背景信息以及你想要完成什么吗?

编辑:下面的第一个版本实际上非常糟糕。这里有一个更好的版本:

lines = []

File.open("mac1.txt",'r').each_line do |a|
  lines.push(a.rstrip!)
end

File.open("mac2.txt",'r').each_line do |b|
  if lines.include?(b.rstrip!)
    puts b
  end
end

我想你要找的是这样的东西:

v = File.open("/RubyDev/sort/mac1.txt",'r').each_line do |a|
      w = File.open("/RubyDev/sort/mac2.txt",'r').each_line do |b|
            if w in v
                puts w
            end
      end
end
File.open("mac1.txt",'r').each_line do |a|
  File.open("mac2.txt",'r').each_line do |b|
    if a == b
      puts b
    end
  end
end
common_macs = File.open("mac1.txt").readlines.map(&:chomp) & 
  File.open("mac2.txt").readlines.map(&:chomp)

对吗?如果没有,你能提供更多关于这个问题的背景信息以及你正在努力完成的任务吗?

我仍在努力学习Ruby,因此这不太可能是一个好的解决方案,但这是一种可能性。它将第一个文件的内容读入散列,然后对照它检查第二个文件的内容。我认为它会相当有效(嗯……除非第一个文件太大,无法很好地放入内存)

编辑为完整起见,以下是马克·托马斯评论中建议的非常简洁的版本,加文·安德雷格建议删除空白。Ruby是一种甜美的语言

lines = Hash.new
File.open( "mac1.txt", 'r' ).each_line {|l| lines[l.strip!] = true}
File.open( "mac2.txt", 'r' ).each_line {|l| puts l if lines[l.strip!]}

我仍在努力学习Ruby,所以这不太可能是一个好的解决方案,但这是一种可能性。它将第一个文件的内容读入散列,然后对照它检查第二个文件的内容。我认为它会相当有效(嗯……除非第一个文件太大,无法很好地放入内存)

编辑为完整起见,以下是马克·托马斯评论中建议的非常简洁的版本,加文·安德雷格建议删除空白。Ruby是一种甜美的语言

lines = Hash.new
File.open( "mac1.txt", 'r' ).each_line {|l| lines[l.strip!] = true}
File.open( "mac2.txt", 'r' ).each_line {|l| puts l if lines[l.strip!]}

要获取两个文件之间的所有公共行,可以使用
File#readlines
以数组形式访问文件中的所有行。请记住,它们仍然会附加新行(“\n”),因此您需要使用
String\chomp
删除它们。最简单的方法是映射
readlines
提供给您的数组,如下所示:

v = File.open("/RubyDev/sort/mac1.txt",'r').each_line do |a|
      w = File.open("/RubyDev/sort/mac2.txt",'r').each_line do |b|
            if w in v
                puts w
            end
      end
end
File.open("mac1.txt",'r').each_line do |a|
  File.open("mac2.txt",'r').each_line do |b|
    if a == b
      puts b
    end
  end
end
common_macs = File.open("mac1.txt").readlines.map(&:chomp) & 
  File.open("mac2.txt").readlines.map(&:chomp)

要获取两个文件之间的所有公共行,可以使用
File#readlines
以数组形式访问文件中的所有行。请记住,它们仍然会附加新行(“\n”),因此您需要使用
String\chomp
删除它们。最简单的方法是映射
readlines
提供给您的数组,如下所示:

v = File.open("/RubyDev/sort/mac1.txt",'r').each_line do |a|
      w = File.open("/RubyDev/sort/mac2.txt",'r').each_line do |b|
            if w in v
                puts w
            end
      end
end
File.open("mac1.txt",'r').each_line do |a|
  File.open("mac2.txt",'r').each_line do |b|
    if a == b
      puts b
    end
  end
end
common_macs = File.open("mac1.txt").readlines.map(&:chomp) & 
  File.open("mac2.txt").readlines.map(&:chomp)

不错。Readmode是默认值,因此可以用
File.open(“mac1.txt”)替换第一节。每行{l |行[l]=true}
。第二个可以是
File.open(“mac2.txt”)。每行{l |如果行[l]}
Huh!真漂亮!它仍然受到“\n”v的影响。但“EOF”问题。也许是这样的?1:
lines=Hash.new
2:
File.open(“mac1.txt”)。每行{l}行[l.rstrip!]=true}
3:
File.open(“mac2.txt”)。每行{l |如果行[l.rstrip!]}
@Gavin,我会使用
行[l.chomp]
,但这只是个人喜好。@Mark Gavin:谢谢你的输入。这是个好消息。我想有一个更干净的解决方案。我真的很喜欢Ruby,因为这些解决方案总体上“感觉”不错,而且可读性也很好。Readmode是默认值,因此可以用
File.open(“mac1.txt”)替换第一节。每行{l |行[l]=true}
。第二个可以是
File.open(“mac2.txt”)。每行{l |如果行[l]}
Huh!真漂亮!它仍然受到“\n”v的影响。但“EOF”问题。也许是这样的?1:
lines=Hash.new
2:
File.open(“mac1.txt”)。每行{l}行[l.rstrip!]=true}
3:
File.open(“mac2.txt”)。每行{l |如果行[l.rstrip!]}
@Gavin,我会使用
行[l.chomp]
,但这只是个人喜好。@Mark Gavin:谢谢你的输入。这是个好消息。我想有一个更干净的解决方案。我真的很喜欢Ruby,因为解决方案总体上“感觉”正确,而且仍然非常可读。很好的解释,但我看不出它是如何计算普通Mac的。很好的解释,但我看不出它是如何计算普通Mac的。Array#include不是很有效。基于散列的解决方案更优越。另外,您不需要在第一个文件中使用bang方法。这两种方法都很合理。事实上,我甚至没有考虑过基于散列的比较想法,直到我看到它之后。谈到Ruby,我自己也是新手,所以我不知道
include?
效率低下。谢谢并不是说
include?
本身效率低下,而是扫描数组寻找匹配的元素是O(n),而散列查找是O(1)。数组#include效率不高。基于散列的解决方案更优越。另外,您不需要在第一个文件中使用bang方法。这两种方法都很合理。事实上,我甚至没有考虑过基于散列的比较想法,直到我看到它之后。谈到Ruby,我自己也是新手,所以我不知道
include?
效率低下。谢谢并不是说
include?
本身效率低下,而是在数组中扫描匹配的元素是O(n),而散列查找是O(1)。