Created
December 31, 2015 13:08
-
-
Save srikanthps/d0ab35b63093945136f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For stackoverflow issue http://stackoverflow.com/questions/34544049/process-a-file-using-hash | |
hash_array = [] | |
# Parse the lines and store it in hash array | |
File.open("b.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*/)} | |
hash_array << splits.to_h | |
end | |
end | |
# Sort the array of hashes | |
hash_array.sort {|i, j| i["name"] <=> j["name"]} | |
# Print the output | |
header = ["Name", "Email", "Country", "Comments"] | |
puts header.join(" ") | |
hash_array.each do |h| | |
puts h.values_at(*header).join(" ") | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment