Ruby 使用哈希处理文件
下面是我想要存储到哈希表中的输入文件,对其进行排序,并以如下所示的格式输出 输入文件Ruby 使用哈希处理文件,ruby,Ruby,下面是我想要存储到哈希表中的输入文件,对其进行排序,并以如下所示的格式输出 输入文件 Name=Ashok, Email=ashok85@gmail.com, Country=India, Comments=9898984512 Email=raju@hotmail.com, Country=Sri Lanka, Name=Raju Country=India, Comments=45535878, Email=vijay@gmail.com, Name=Vijay Name=Ashok, Co
Name=Ashok, Email=ashok85@gmail.com, Country=India, Comments=9898984512
Email=raju@hotmail.com, Country=Sri Lanka, Name=Raju
Country=India, Comments=45535878, Email=vijay@gmail.com, Name=Vijay
Name=Ashok, Country=India, Email=ashok37@live.com, Comments=8898788987
输出文件(按名称排序)
到目前为止,我已经从文件中读取了数据,并将每一行存储到一个数组中,但我仍然停留在hash[key]=>value上
file_data = {}
File.open('input.txt', 'r') do |file|
file.each_line do |line|
line_data = line.split('=')
file_data[line_data[0]] = line_data[1]
end
end
puts file_data
假设输入文件中的每一行都有由逗号分隔的
key=value
字符串模式,则需要先围绕逗号拆分该行,然后围绕等号拆分该行。以下是已更正代码的版本:
# Need to collect parsed data from each line into an array
array_of_file_data = []
File.open('input.txt', 'r') do |file|
file.each_line do |line|
#create a hash to collect data from each line
file_data = {}
# First split by comma
pairs = line.chomp.split(", ")
pairs.each do |p|
#Split by = to separate out key and value
key_value = p.split('=')
file_data[key_value[0]] = key_value[1]
end
array_of_file_data << file_data
end
end
puts array_of_file_data
下面给出了程序的更完整版本
hash_array = []
# Parse the lines and store it in hash array
File.open("sample.txt", "r") do |f|
f.each_line do |line|
# Splits are done around , and = preceded or followed
# by any number of white spaces
splits = line.chomp.split(/\s*,\s*/).map{|p| p.split(/\s*=\s*/)}
# to_h can be used to convert an array with even number of elements
# into a hash, by treating it as an array of key-value pairs
hash_array << splits.to_h
end
end
# Sort the array of hashes
hash_array = hash_array.sort {|i, j| i["Name"] <=> j["Name"]}
# Print the output, more tricks needed to get it better formatted
header = ["Name", "Email", "Country", "Comments"]
puts header.join(" ")
hash_array.each do |h|
puts h.values_at(*header).join(" ")
end
您可能想参考一下更具格式的表格输出我试过了,但没有告诉您免费做作业。我从文件中读取数据并将每一行存储到数组中,但是我被困在hash[key]=>valuefile\u data={}file.open('input.txt','r')do | file | file.each|line do | line | line | line | data=line.split('=')file\u data[line\u data[0]]=line\u data[1]结束文件_data@anjalirai下一次,请分享您的代码,以便其他人可以进一步指导。我已将您的代码添加到您的问题中。如果需要,请随时编辑您的问题以添加更多详细信息。希望这些电子邮件(和潜在的手机号码)在您的控制之下……在互联网上这样发布是非常危险的。谢谢。。下次我将分享正确的代码排序部分有一个错误,现在修复。
{"Name"=>"Ashok", "Email"=>"ashok85@gmail.com", "Country"=>"India", "Comments"=>"9898984512"}
{"Email"=>"raju@hotmail.com", "Country"=>"Sri Lanka", "Name"=>"Raju"}
{"Country"=>"India", "Comments"=>"45535878", "Email"=>"vijay@gmail.com", "Name"=>"Vijay"}
{"Name"=>"Ashok", "Country"=>"India", "Email"=>"ashok37@live.com", "Comments"=>"8898788987"}
hash_array = []
# Parse the lines and store it in hash array
File.open("sample.txt", "r") do |f|
f.each_line do |line|
# Splits are done around , and = preceded or followed
# by any number of white spaces
splits = line.chomp.split(/\s*,\s*/).map{|p| p.split(/\s*=\s*/)}
# to_h can be used to convert an array with even number of elements
# into a hash, by treating it as an array of key-value pairs
hash_array << splits.to_h
end
end
# Sort the array of hashes
hash_array = hash_array.sort {|i, j| i["Name"] <=> j["Name"]}
# Print the output, more tricks needed to get it better formatted
header = ["Name", "Email", "Country", "Comments"]
puts header.join(" ")
hash_array.each do |h|
puts h.values_at(*header).join(" ")
end
Name Email Country Comments
Ashok ashok85@gmail.com India 9898984512
Ashok ashok37@live.com India 8898788987
Raju raju@hotmail.com Sri Lanka
Vijay vijay@gmail.com India 45535878