Reputation: 165
I have a products file listing the item #, products, and price. I would like to read this file and initialize it as a hash with the item #'s being the key and the products and price being the value. Here is my file
199, Shoes, 59.99
211, Shirts, 19.99
245, Hats, 25.99
689, Coats, 99.99
712, Beanies, 6.99
I would like it to look like this.
products = {
199 =>['Shoes', 59.99],
211 =>['Shirts', 19.99],
245 =>['Hats', 25.99],
689 => ['Coats', 99.99],
712 => ['Beanies', 6.99]
}
This is what i could come up with which is not really what it want.
products_file = File.open("files.txt")
products_hash = []
while ! products_file.eof?
product_hash = products_file.gets.chomp
print product_hash.split(', ')
end
And here is the output that I came up with:
["199", "Shoes", "59.99"]
["211", "Shirts", "19.99"]
["245", "Hats", "25.99"]
["689", "Coats", "99.99"]
["712", "Beanies", "6.99"]
Upvotes: 3
Views: 3333
Reputation: 80065
A variation, manipulating the data with CSV's converters:
require 'csv'
products = {}
CSV.foreach('products.csv', {col_sep: ', ', converters: :numeric}) do |row|
products[row.shift] = row
end
p products
#=> {199=>["Shoes", 59.99], 211=>["Shirts", 19.99], 245=>["Hats", 25.99], 689=>["Coats", 99.99], 712=>["Beanies", 6.99]}
Upvotes: 1
Reputation: 66837
I saved your data as a CSV file called products.csv
and did this:
require 'csv'
products = {}
CSV.foreach("products.csv") do |line|
products[line[0].to_i] = [line[1].strip, line[2].to_f]
end
products
#=> {199=>["Shoes", 59.99], 211=>["Shirts", 19.99], 245=>["Hats", 25.99], 689=>["Coats", 99.99], 712=>["Beanies", 6.99]}
The same result can be achieved in a more concise way using each_with_object
, but it reads the whole file into memory at once, which may not be a good idea if the file is large:
require 'csv'
products = CSV.read("products.csv").each_with_object({}) do |line, h|
h[line[0].to_i] = [line[1].strip, line[2].to_f]
end
There's also a more functional approach, as originally suggested by Phrogz:
require 'csv'
products = Hash[ CSV.read('products.csv').map do |row|
[ row[0].to_i, [row[1].strip,row[2].to_f] ]
end ]
Upvotes: 11