我需要从如下所示的.txt文件中找到唯一的姓氏:
Kent, Mackenna -- ut
Voluptatem ipsam et at.
Marven, Gardens -- non
Facere et necessitatibus animi.
McLaughlinn, Matt -- consequatur
Eveniet temporibus ducimus amet eaque.
Lang, August -- pariatur
Unde voluptas sit fugit.
Brad, Nick -- et
Maiores ab officia sed.
Adam, Levine -- error
Repellendus alias officia amet et perspiciatis.
Learner, York -- nesciunt
Incidunt et ut necessitatibus porro.
Ortiz, Andrew -- fuga
Tempore eos et hic.
Lang, Bryant -- et
Laborum perferendis inventore eveniet.
到目前为止,我有:
FNAME = 'example.txt'
# maps for last name in file
def last_name_from_file(file_name)
last_name = File.readlines(file_name).reject(&:empty?)
last_name.map do |line|
line.split.first
end
end
puts last_name_from_file('example.txt')
但是这给了我输出,其中包括Lorem文本和姓氏。
#Kent
#Voluptatem
#Marven
#Facere
#McLaughlinn
#Eveniet
#Lang
#Unde
#Brad
...
答案 0 :(得分:1)
我看到lorem行是偶数,所以您可以拒绝它们。
def last_name_from_file(file_name)
File.
readlines(file_name).
reject.
with_index(1) { |_, id| id.even? }.
map { |line| line.split(',').first }.
uniq
end
好吧,我该如何获得名字?
您可以执行以下方法:
def names(file_name)
File.
readlines(file_name).
reject.
with_index(1) { |_, id| id.even? }.
map { |line| line.split(' --').first.split(', ') }.
map { |ln, fn| { lastname: ln, firstname: fn } }
end
现在您可以调用它:
names = names('example.txt')
names
# => [{:lastname=>"Kent", :firstname=>"Mackenna"}, {:lastname=>"Marven", :firstname=>"Gardens"}, {:lastname=>"McLaughlinn", :firstname=>"Matt"}, {:lastname=>"Lang", :firstname=>"August"}, {:lastname=>"Brad", :firstname=>"Nick"}, {:lastname=>"Adam", :firstname=>"Levine"}, {:lastname=>"Learner", :firstname=>"York"}, {:lastname=>"Ortiz", :firstname=>"Andrew"}, {:lastname=>"Lang", :firstname=>"Bryant"}]
names.map { |name| name[:lastname] }
# => ["Kent", "Marven", "McLaughlinn", "Lang", "Brad", "Adam", "Learner", "Ortiz", "Lang"]
names.map { |name| name[:firstname] }
# => ["Mackenna", "Gardens", "Matt", "August", "Nick", "Levine", "York", "Andrew", "Bryant"]
答案 1 :(得分:0)
类似于昨天的my answer:
def last_name_from_file(fname)
File.open(fname, "r").each_with_object([]).with_index do |(line, o), i|
o << line.split(',').first if i.even?
end.uniq
end
puts last_name_from_file('example.txt')
#Kent
#Marven
#McLaughlinn
#...
答案 2 :(得分:0)
让我们创建文件。
FName = 'temp.txt'
IO.write(FName,
<<~END
Kent, Mackenna -- ut
Voluptatem ipsam et at.
Marven, Gardens -- non
Facere et necessitatibus animi.
McLaughlinn, Matt -- consequatur
Eveniet temporibus ducimus amet eaque.
Lang, August -- pariatur
Unde voluptas sit fugit.
Brad, Nick -- et
Maiores ab officia sed.
Adam, Levine -- error
Repellendus alias officia amet et perspiciatis.
Learner, York -- nesciunt
Incidunt et ut necessitatibus porro.
O'Conner-Bolonzo, Andrew -- fuga
Tempore eos et hic.
Lang, Bryant -- et
Laborum perferendis inventore eveniet.
END
)
#=> 539
我们可以通过逐行读取文件,逐行读取来提取唯一的姓氏;无需将文件插入数组。
require 'set'
IO.foreach(FName).each_with_object(Set.new) do |line,set|
last_name = line[/\A[\p{Alpha}'-]+(?=,)/]
set << last_name unless last_name.nil?
end.to_a
#=> ["Kent", "Marven", "McLaughlinn", "Lang", "Brad", "Adam", "Learner",
# "O'Conner-Bolonzo"]
请参见IO::foreach,Enumerable#each_with_object,Set::new,Set#to_a和String#[]。
请注意,IO::foreach
在未分配块时会返回一个枚举数,因此可以在此处链接到Enumerable#each_with_object
。
String#[]
使用的正则表达式读取“立即匹配字符串(+
)开头的一个或多个(\A
)字符,即字母,撇号或连字符然后是逗号。”