使用 Ruby 查找 X 文件中的公共行
现在我有 3 个文件,但我想以一种稍后可以添加更多文件的方式执行此操作。每个文件都是一个 ID 列表,如下所示。
174535945 174538045 160515924 81712260 25241494
我希望输出是列表 1 和列表 2、列表 2 和列表 3 中出现的项目,以及列表 1、列表 2 和列表 3 中出现的项目。
创建哈希的最红宝石方式是每个列表都有一个密钥,然后获取所有密钥并针对所有哈希进行测试,或者是否有一个很好的 gem 可以帮助解决这个问题?
谢谢,
Right now I have 3 files, but I'd like to do this in a way that I can add more later. Each file is a list of IDs, like this.
174535945
174538045
160515924
81712260
25241494
I'd like the output to be the items that appear in list 1 and list 2, list 2 and list 3, and those that occur in list 1 and list 2 and list 3.
Would the most ruby way to be create a hash with a key for each list, and then get all keys and test against all hashes, or is there a nice gem that would help with this?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用套装:
Use sets:
我认为您使用哈希的建议是可行的方法:
I think your suggestion of using a hash is the way to go: