在Ruby中将文件逐行拆分到多个数组中

在Ruby中将文件逐行拆分到多个数组中,ruby,Ruby,下面是文件,条目以选项卡分隔 sample.txt id\a_po87y Jack leader id\ruio66 John_Marsha leader id\rzd766 123_Smart_option fresher ....... ........ etc 我试图将这些值拆分为3个数组,如下所示 id = ["id\a_po87y", "id\ruio66", "id\rzd766", ....] store = ["Jack", "John_Marsha", "123_Smart

下面是文件,条目以选项卡分隔

sample.txt

id\a_po87y Jack leader
id\ruio66 John_Marsha leader
id\rzd766 123_Smart_option fresher
.......
........
etc
我试图将这些值拆分为3个数组,如下所示

id = ["id\a_po87y", "id\ruio66", "id\rzd766", ....]

store = ["Jack", "John_Marsha", "123_Smart_option", ....]

group = ["leader", "leader", "fresher", ....]
下面是我的不完整代码

id = Array.new
store = Array.new
group = Array.new
File.open("D:\sample.txt", "r").each_line do |line|
    id << line.split("\t")
    #not sure how to cpature the remaining entries
end
好像你在找什么。试试这个

file = File.open("D:\sample.txt", "r")
file.each_line.map { |line| line.split("\t") }.transpose.each do |fields|
  puts fields
end

好极了我没想到会有转置法,谢谢。我尝试使用-
字段。首先
提取id并分配给一个名为id的数组,但我无法分离字段。我尝试如下
id=[“id\a\u po87y”、“id\ruio66”、“id\rzd766”、…]存储=[“Jack”、“John\u Marsha”、“123\u Smart\u option”、…]组=[“leader”、“leader”、“fresh”、…]
对不起,伙计,我不明白。你能打开另一个问题并输入代码吗?对不起,我不能在90分钟内发布另一个问题。我已经编辑了这个问题,并在“输出应该是”一节下询问了我的问题。谢谢,againI尝试了类似
file=file.open('./file');fields=file.each_line.map{| line | line.split(“\t”)};id,store=fields
,它对我有效。让我知道它的输出是puts id
id\a_po87y Jack leader
puts store
id\ruio66 John_Marsha leader
但我正在寻找puts id
id\a_po87y id\ruio66 id\rzd766
和puts store,应该是
Jack John_Marsha 123_Smart_option
file = File.open("D:\sample.txt", "r")
file.each_line.map { |line| line.split("\t") }.transpose.each do |fields|
  puts fields
end