Skip to content

Instantly share code, notes, and snippets.

@srikanthps
Created December 31, 2015 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srikanthps/d0ab35b63093945136f7 to your computer and use it in GitHub Desktop.
Save srikanthps/d0ab35b63093945136f7 to your computer and use it in GitHub Desktop.
# 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