使用哈希处理文件

时间:2015-12-31 09:19:28

标签: 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, Country=India, Email=ashok37@live.com, Comments=8898788987

输出文件(按名称排序)

Name Email Country Comments
-------------------------------------------------------
Ashok ashok37@live.com India 8898788987
Ashok ashok85@gmail.com India 9898984512
Raju raju@hotmail.com Sri Lanka
Vijay vijay@gmail.com India 45535878

到目前为止,我已经从文件中读取数据并将每一行存储到一个数组中,但我仍然停留在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

1 个答案:

答案 0 :(得分:0)

鉴于输入文件中的每一行都有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

以上代码将打印:

{"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

您可能希望引用Padding printed output of tabular data以获得更好的格式化表格输出