tags:

views:

52

answers:

1

I am dealing with a csv file that has some customer information (email, name, address, amount, [shopping_list: item 1, item 2]).

I would like work with the data and produce some labels for printing... as well as to gather some extra information (total amounts, total items 1...)

My main concern is to find the appropriate structure to store the data in ruby for future manipulation. For now I have thought about the following possibilities:

  1. multidimensional arrays: pretty simple to build, but pretty hard to access the data in a beautiful ruby way.
  2. hashes: having the email as key, and storing the information in different hashes (one hash for name, another hash for address, another hash for shopping list...)
  3. (getting the cvs data in to a Database and working with the data from ruby??)

I would really appreciate your advice and guidance!!

+2  A: 

Once you have more than a couple pieces of information that you need to group together, it's time to consider moving from a generic hash/array to something more specialized. A good candidate for what you've described is Ruby's struct module:

Customer = Struct.new(:email, :name, :address) # etc.

bill = Customer.new('[email protected]', 'Bill Foo', '123 Bar St.')

puts "#{bill.name} lives at #{bill.address} and can be reached at #{bill.email}"

Output:

Bill Foo lives at 123 Bar St. and can be reached at [email protected]

Struct#new simply creates a class with an attr_accessor for each symbol you pass in. Well, it actually creates a bit more than that, but for starters, that's all you need to worry about.

Once you've got the data from each row packed into an object of some sort (whether it's a struct or a class of your own), then you can worry about how to store those objects.

  • A hash will be ideal for random access by a given key (perhaps the customer's name or other unique ID)
  • A one-dimensional array works fine for iterating over the entire set of customers in the same order they were inserted
Mark Rushakoff
Thanks for your super-quick reply :)If I understand correctly, what I should be doing is: 1st getting each "line" of the cvs in some sort of Struct/Class and 2nd store all the Structs in a hash/array to facilitate manipulation... is that correct?
ischnura
@ischnura: Yes, you're exactly correct in your understanding.
Mark Rushakoff
Thanks again for your answer... I have structured the code as you suggested and everything is much simpler to understand. Everything is working great!
ischnura